我会尽力给你尽可能详细的信息。
我创建了一个平台游戏,我已经实现了移动和碰撞。
我的游戏循环运行此(我只有一个eventListener)
private function level1GameLoop():void
{
groundCollision();
movementCode();
scrollLevel();
}
地面碰撞负责确保玩家不会摔倒(地面是整体水平,为不良命名惯例道歉。
private function groundCollision():void
{
if (ground.bottom.hitTestPoint(character.x + 13, character.y, true || ground.bottom.hitTestPoint(character.x + 13, character.y, true)))
{
trace("h");
while (ground.bottom.hitTestPoint(character.x + 13, character.y, true || ground.bottom.hitTestPoint(character.x + 13, character.y, true)))
{
character.incrementUp();
if (ground.bottom.hitTestPoint(character.x + 13, character.y, true || ground.bottom.hitTestPoint(character.x + 13, character.y, true)))
{
}
else{
character.keepOnGround();
}
}
}
}
然后你有典型的移动代码
else if (rightKey)
{
character.moveRight();
lookingRight = true;
lookingLeft = false;
}
else if (leftKey)
{
lookingLeft = true;
lookingRight = false;
character.moveLeft();
}
在滚动关卡之前,我测试过看到我的角色是否击中了所有平台,是的,确实是这样。
所以现在我想实现滚动。
在类定义中,我设置了一个名为container的变量,它是一个精灵。
private var container:Sprite = new Sprite();
然后我在开始游戏循环之前将我的孩子添加到舞台上
addChild(container);
container.addChild(ground);
container.addChild(character);
addChild(timerGIF);//this does not need to scroll
然后我再次测试了游戏,碰撞工作正常。
现在我尝试了多次尝试滚动播放器并让他保持在屏幕中央,确保碰撞仍能正常运行。
私有函数scrollLevel():void {
/*
_camera.x = character.x - stage.stageWidth * 0.5;
_camera.y = character.y - stage.stageHeight * 0.5;
//"camera" must not show areas outside of the game world
if (_camera.x < 0)
{
_camera.x = 0;
}
//Right
if (_camera.x > (container.width) - stage.stageWidth)
{
_camera.x = (container.width) - stage.stageWidth;
}
//Bottom
if (_camera.y > (container.height) - stage.stageHeight)
{
_camera.y = (container.height) - stage.stageHeight;
}
//Top
if (_camera.y < 0)
{
_camera.y = 0;
}
container.scrollRect = _camera;
*/
//ground.x = -character.x + stage.stageWidth / 2;
// ground.y = -character.y + stage.stageHeight / 2;
/*var offset:int = 10;
var distance:Number = character.x - ((stage.stageWidth / 2) + offset);
var distance2:Number = character.y - ((stage.stageHeight / 2));
var ease:int = 5;
if (distance < 0)
{
distance *= -1;
}
if (distance2 < 0)
{
distance2 *= -1;
}
if (character.x < (stage.stageWidth / 2))
{
var variable:int = distance / ease;
//character.x += variable;
container.x += variable;
}
if (character.x > (stage.stageWidth / 2))
{
var variable2:int = distance / ease;
//character.x -= variable2;
container.x -= variable2;
}
/*if (character.y < (stage.stageHeight / 2))
{
var variable3:int = distance2 / ease;
character.y += variable3;
//container.y += variable3;
}
if (character.y > (stage.stageHeight / 2))
{
var variable4:int = distance2 / ease;
character.y -= variable4;
//container.y -= variable4;
}*/
/*var camera:Point = new Point();
// Set the camera coordinates to the char coordinates.
camera.x = character.x;
camera.y = character.y;
// Adjust the world position on the screen based on the camera position.
ground.x = -camera.x + (stage.stageWidth / 2);
ground.y = -camera.y + (stage.stageHeight / 2);*/
}
这些似乎都不起作用,因为碰撞会变得混乱。
我的意思是,我的角色最终会浮动,或者通过
坠落其他要提的是,地面是一个影片剪辑,基本上是我的水平(再次,糟糕的命名惯例,apoligiez)。
在地面内有hitTest框和角色将获得的对象。
答案 0 :(得分:1)
你做得很好,但是以非常困难的方式,首先要做的事情,你应该知道当你想要你的角色在某个方向上移动到容器内时,你必须调整所有其他的孩子。容器向相反的方向移动,所以它给人的感觉是角色在所需的方向上移动..这是我的朋友是艰难的方式,我认为你走的那样的原因是你想要timerGIF
永远不要改变..但这可以通过颠倒你的代码以更简单的方式实现......
//reverse your strategy for coding:
var container:Sprite = new Sprite();
addChild(container);
addChild(ground);
addChild(character);
container.addChild(timerGIF);
//define some very imp vars that you will need to adjust for each level to
//prevent recoding in each level, just set the vars to the new level properties
var level_width:Number=1600;
var level_hight:Number=1200;
var screen_width:Number=800;
var screen_hight:Number=600;
//the ease of camera is the speed of camera, and whoever you want the camera to
//follow just pass it as a camera_target (that will be in this case your character)
var camera_speed:Number=5;
var camera_target:Object=charachter;
//then add your scroll function (named hear as camera();)
function camera(wide, high, target, speed) {
if (target!=null) {
cam_x = ((screen_width/2)-target.x-x)/speed;
cam_y = ((screen_hight/2)-target.y-y)/speed;
x+=cam_x;
y+=cam_y;
}
if (x>0) {
x=0;
}
if (x<wide*-0.5) {
x=- wide/2;
}
if (y>0) {
y=0;
}
if (y<high*-0.5) {
y=- high/2;
}
//now the code to keep container and all its children not scrolled is just:
container.x=- x;
container.y=- y;
}
最后在level1GameLoop()
替换scrollLevel();
function level1GameLoop():void
{
groundCollision();
movementCode();
//scrollLevel();
camera (level_width, level_hight, camera_target, camera_speed);
}
祝你生日愉快......
答案 1 :(得分:0)
检查此代码,是否为starling,但您可以使用逻辑