我正在尝试在AS3中制作一个简单的游戏,类似于Tilt To Live。 我试着按照我的鼠标(另一个球)做一个球,所以当球接触到Mouseball时你会失败。 它一直在给错误,我真的不知道如何解决它。
到目前为止,这是我的游戏:
public class Main扩展Sprite {
public function Main():void
{
var vijand:Sprite = new Sprite ();
vijand.graphics.beginFill(0xFFFF66) ;
vijand.graphics.drawCircle(30,30 ,30 );
addChild(vijand);
var vijand1:Sprite = new Sprite ();
vijand1.graphics.beginFill(0xFFFF66) ;
vijand1.graphics.drawCircle(50,50 ,50 );
addChild(vijand1);
}
public function beweeg(e:Event.):void
{
vijand.x = mouseX;
vijand.y = mouseY;
if (vijand.x > vijand1.x)
{
vijand1.x += 2;
}
if (vijand.y > vijand1.y)
{
vijand1.y += 2;
}
if (vijand.x < vijand1.x)
{
vijand1.x -= 2;
}
if (vijand.y < vijand1.y)
{
vijand1.y -= 2;
}
}
}
}
答案 0 :(得分:0)
我可以看到的一个问题是你的'vijand'和'vijand1'变量是你的Main构造函数的本地变量。如果要在Main构造函数之外访问它们,则必须在构造函数之外声明它们,例如:
private var vijand:Sprite = new Sprite();
private var vijand1:Sprite = new Sprite();
public function Main():void
{
....
“beweeg”功能中也有拼写错误。除掉 '。'在“活动”之后:
public function beweeg(e:Event):void
{
....
最后,您还需要一个事件监听器来触发beweeg函数。可能是Main构造函数中的一个ENTER_FRAME监听器:
addEventListener(Event.ENTER_FRAME, beweeg);
答案 1 :(得分:0)
您应该为移动对象创建正确的逻辑。这是你的例子,实际上很有趣,我发布之前已经玩了一段时间
var myBall:Shape = new Shape();
var enemyBall:Shape = new Shape();
var ballRadius:int = 8;
var dx:Number, dy:Number, radian:Number, speed:Number = 10;
myBall.graphics.beginFill(0x009900);
myBall.graphics.drawCircle(0, 0, ballRadius);
enemyBall.graphics.beginFill(0x990000);
enemyBall.graphics.drawCircle(0, 0, ballRadius);
addChild(myBall);
addChild(enemyBall);
//Place enemy at center
enemyBall.x = stage.stageWidth * 0.5;
enemyBall.y = stage.stageHeight * 0.5;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(e:Event):void {
myBall.x = stage.mouseX;
myBall.y = stage.mouseY;
dx = myBall.x - enemyBall.x;
dy = myBall.y - enemyBall.y;
if (Math.sqrt(dx * dx + dy * dy) <= ballRadius * 2) {
//NOOOOOoooooo
//Game over
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
} else {
//Update enemy position
//Every N ticks, increase speed, because it will be very easy game ;)
radian = Math.atan2(dy, dx);
enemyBall.x += Math.cos(radian) * speed;
enemyBall.y += Math.sin(radian) * speed;
}
}