我正在为我的班级制作一个类似于Space Invaders / Galaga的游戏。我是ActionScript和编码的新手,我已经创建了一个敌人类,我将它生成,然后向x方向移动。我只是想知道如果让我的敌人在我的舞台结束时(700x500,然后继续前进到另一边)让我的敌人向下移动,我假设在我的敌人类中种植一个if语句,不确定如何去做,任何帮助都会做,非常感谢大家。
敌人类
package
{
import flash.display.MovieClip;
public class Enemy extends MovieClip
{
public function Enemy()
{
x = 60;
y = 30;
}
public function moveDownABit():void
{
}
public function moveRight():void
{
x = x + 2;
}
public function moveDown():void
{
}
public function moveLeft():void
{
}
public function moveUp():void
{
}
}
}
游戏
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class SpaceVigilanteGame extends MovieClip
{
public var enemy:Enemy;
public var avatar:Avatar;
public var gameTimer:Timer;
var gameWidth:int = 0;
var gameHeight:int = 0;
public function SpaceVigilanteGame()
{
enemy = new Enemy();
addChild( enemy );
avatar = new Avatar();
addChild( avatar );
gameWidth = stage.stageWidth;
gameHeight = stage.stageHeight;
gameTimer = new Timer( 25 );
gameTimer.addEventListener( TimerEvent.TIMER, moveEnemy );
gameTimer.start();
}
public function moveEnemy( timerEvent:TimerEvent ):void
{
//enemy.moveDownABit();
if(enemy.x+enemy.width+2<=gameWidth)
{
enemy.moveRight();
}
else if(enemy.y+enemy.height+2<=gameHeight)
{
enemy.moveDown();
}
else if(enemy.x-2>=0)
{
enemy.moveLeft();
}
else if(enemy.y-2>=0)
{
enemy.moveUp();
}
}
}
}
答案 0 :(得分:0)
这是非常基本的东西。
让我们假设您的Enemy类中有4个函数moveDown, moveUp, moveRight and moveLeft.
我们需要知道敌人将要移动的区域的宽度和高度,或者通过硬编码(稍后会咬你的屁股),或者通过这样做更加动态:
var gameWidth:int = stage.stageWidth;
var gameHeight:int = stage.stageHeight;
stage.stageWidth
或其高度对应部分会以缩放等方式为您提供SWF电影的大小(以像素为单位)。
您的文档类中已经设置了Timer
,每80ms执行一次函数moveEnemy
。另一种方法是使用Event.ENTER_FRAME
并在每一帧执行一个函数。
现在想一想,每次调用你的函数时,敌人都会向下移动一下。正如你所说,应该有某种......检查机制。但我们不应该把它放在敌人类中,敌人不需要知道何时停止,这就是Document类的问题。理想情况下,你应该能够在各种类中使用那个敌人,对吗?因此,当文档类可以代替时,给予敌人边界将是很多工作。
解决方案非常简单,每次想要移动敌人时,都需要根据舞台的尺寸来检查敌人的位置。
本质:
private var gameWidth:int = 0;
private var gameHeight:int = 0;
public function SpaceVigilanteGame(){
//...your init code for enemy
gameWidth = stage.stageWidth;
gameHeight = stage.stageHeight;
//...your timer code
}
public function moveEnemy(timerEvent:TimerEvent):void{
//lets first check if we can go to the right
if(enemy.x+enemy.width+2<=gameWidth){
//so, we take the right outermost border of your enemy by taking its x-position and adding its width. To check if a step right would be out of bounds, we add the amount it WOULD move
//as an example, your enemy has the x-position 100, and is 200 pixel wide, is 100+200+2 smaller or equal to 700 (your stage width)? Yes it is, thus you can move it
enemy.moveRight();
}
else if(enemy.y+enemy.height+2<=gameHeight){
//but what happens if we can't go right? we jump to this additional if condition that will check if the enemy can move down
//same thing as above, but instead we use y-position and the heights
enemy.moveDown();
}
else if(enemy.x-2>=0){
//if we can't move right or down, try left instead
//this time we only need the x-position of the enemy, because we need to look at the leftmost border
enemy.moveLeft();
}
else if(enemy.y-2>=0){
//same deal with going up
enemy.moveUp();
}
}
这是未经测试的代码,但我希望您能获得此示例的一般要点。这里的问题是,敌人将始终尝试先向前移动。当你从x:0,y:0开始时,他会向右移动直到他到达终点,然后向下移动直到他到达终点,然后向左和向右移动。
运动101:
x = x + 1
x = x - 1
y = y - 1
y = y + 1