Actionscript 3 游戏:对象重生
Actionscript 3 game: object respawning
我正在尝试在 AS3 中制作一个简单的游戏,玩家可以吃掉尽可能多的球。我不知道如何很好地编码,每次吃一个球时我都很难尝试在舞台上添加一个新球。
这是我目前在 main.as 中的代码。
private var startX:Number = 512;
private var startY:Number = 384;
private var speed:Number = 8;
var player1;
var player2;
var player3;
var theBall;
player1 = new player(50,384, 1);
player2 = new player(944,384,2);
player3 = new player(488,84,3);
stage.addChild(player1);
stage.addChild(player2);
stage.addChild(player3);
if(theBall.hitTestObject(player1) || theBall.hitTestObject(player2) || theBall.hitTestObject(player3))
{
//removes the ball from the stage
trace("a player has eaten a ball");
stage.removeChild(theBall);
//adds new ball
//stage.addChild(theBall);
//reset x and y
startX = Math.random()*speed-speed/2;
startY = Math.random()*speed-speed/2;
}
在 ball.as 中我指定了球应该如何随机移动,从舞台中央开始并从墙壁反弹。
没有出现错误,只是代码不起作用。当一个球被吃掉时,你如何让一个新球重新出现在舞台中央?我是在主要的地方还是在 ball.as?
中声明这个?
如果您有一个 "ball" 对象,就像您有一个 "player" 对象一样,那么...
//removes the ball from the stage
trace("a player has eaten a ball");
stage.removeChild(theBall);
//reset x and y
startX = Math.random()*speed-speed/2;
startY = Math.random()*speed-speed/2;
//adds new ball
theBall = new ball();
theBall.x = stage.stageWidth/2;
theBall.y = stage.stageHeight/2;
stage.addChild( theBall );
您重复使用了 "theBall" 变量,因为您已经从舞台上移除了被吃掉的实例。旧的 "theBall" 实例被 stage.removeChild(theBall) 抛出,然后你制作一个新实例并将新实例添加到舞台上。
在你的代码之前,如果 BALL 没有为你的舞台调用和准备。它可能不起作用。
在您的库中,检查包含球的 movie_clip...并在电影剪辑名称旁边的 "AS LINKAGE" 行下...您应该将其命名为 "BALL_TEST"
在您的 AS3 代码中:
在您的代码前添加这个
var ball_bounce:BALL_TEST = new BALL_TEST();
然后你的代码
如果我没有正确理解你的问题...
亲切的问候
我正在尝试在 AS3 中制作一个简单的游戏,玩家可以吃掉尽可能多的球。我不知道如何很好地编码,每次吃一个球时我都很难尝试在舞台上添加一个新球。 这是我目前在 main.as 中的代码。
private var startX:Number = 512;
private var startY:Number = 384;
private var speed:Number = 8;
var player1;
var player2;
var player3;
var theBall;
player1 = new player(50,384, 1);
player2 = new player(944,384,2);
player3 = new player(488,84,3);
stage.addChild(player1);
stage.addChild(player2);
stage.addChild(player3);
if(theBall.hitTestObject(player1) || theBall.hitTestObject(player2) || theBall.hitTestObject(player3))
{
//removes the ball from the stage
trace("a player has eaten a ball");
stage.removeChild(theBall);
//adds new ball
//stage.addChild(theBall);
//reset x and y
startX = Math.random()*speed-speed/2;
startY = Math.random()*speed-speed/2;
}
在 ball.as 中我指定了球应该如何随机移动,从舞台中央开始并从墙壁反弹。
没有出现错误,只是代码不起作用。当一个球被吃掉时,你如何让一个新球重新出现在舞台中央?我是在主要的地方还是在 ball.as?
中声明这个?如果您有一个 "ball" 对象,就像您有一个 "player" 对象一样,那么...
//removes the ball from the stage
trace("a player has eaten a ball");
stage.removeChild(theBall);
//reset x and y
startX = Math.random()*speed-speed/2;
startY = Math.random()*speed-speed/2;
//adds new ball
theBall = new ball();
theBall.x = stage.stageWidth/2;
theBall.y = stage.stageHeight/2;
stage.addChild( theBall );
您重复使用了 "theBall" 变量,因为您已经从舞台上移除了被吃掉的实例。旧的 "theBall" 实例被 stage.removeChild(theBall) 抛出,然后你制作一个新实例并将新实例添加到舞台上。
在你的代码之前,如果 BALL 没有为你的舞台调用和准备。它可能不起作用。
在您的库中,检查包含球的 movie_clip...并在电影剪辑名称旁边的 "AS LINKAGE" 行下...您应该将其命名为 "BALL_TEST"
在您的 AS3 代码中:
在您的代码前添加这个
var ball_bounce:BALL_TEST = new BALL_TEST();
然后你的代码
如果我没有正确理解你的问题...
亲切的问候