我创造了一个空间道奇' AS3中的游戏,一切顺利,然而,我发现它太容易了。 '小行星'只有在最后一个击中边缘后进来。我希望游戏让更多的小行星向你走来,但是当我嵌入另一个图像时,它似乎给我一个错误。代码在这里:
package
{
import flash.display.Bitmap;
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.ui.Keyboard;
import flash.utils.*;
import flash.text.TextFormatAlign;
/**
* ...
* @author DogeGames Studios
*/
public class Main extends MovieClip
{
var text:TextField = new TextField();
var format:TextFormat = new TextFormat();
var stext:TextField = new TextField();
var sform:TextFormat = new TextFormat();
var score:Number = 0;
var stageRef:Stage
public var inair:int = 1;
[Embed(source = 'img/char.png')]
var charClass:Class;
var char:Bitmap = new charClass();
[Embed(source = 'img/rock.png')]
var rockClass:Class;
var rock:Bitmap = new rockClass();
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private var moveTimer:Timer;
private var hitTimer:Timer;
private var scoreTimer:Timer;
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
addChild(rock);
addChild(rock);
addChild(rock);
scoreTimer = new Timer(1000, 0);
scoreTimer.addEventListener(TimerEvent.TIMER, addScore);
scoreTimer.start();
hitTimer = new Timer(25, 0);
hitTimer.addEventListener(TimerEvent.TIMER, hitDetect);
hitTimer.start();
moveTimer = new Timer(25, 0);
moveTimer.addEventListener(TimerEvent.TIMER, moveRock);
moveTimer.start();
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveShip);
var watermark:TextField = new TextField();
watermark.text = "DogeGames.cu.cc";
var wform:TextFormat = new TextFormat();
wform.align = TextFormatAlign.RIGHT;
wform.size = 24;
wform.font = "Comic Sans MS";
watermark.setTextFormat(wform);
text.text = "Space Dodger - By Dogegames Studios ";
char.x = 50;
char.y = 482;
rock.x = 850;
rock.y = Math.random() * 600;
format.size = 24;
format.font = "Comic Sans MS";
text.setTextFormat(format);
text.autoSize = TextFieldAutoSize.LEFT;
sform.font = "Comic Sans MS";
sform.size = 24;
sform.align = TextFormatAlign.RIGHT;
addChild(text);
addChild(char);
}
function moveShip(e:MouseEvent):void {
if (char.y >= 600) {
char.y = 500;
} else {
char.y = mouseY;
}
}
private var speed:Number = 15;
function hitDetect(e:TimerEvent):void {
if (char.getBounds(stage).intersects(rock.getBounds(stage))) {
text.text = "GAME OVER!";
text.setTextFormat(format);
moveTimer.stop();
hitTimer.stop();
scoreTimer.stop();
}
}
function addScore(e:TimerEvent):void {
score++;
stext.text = "Score: " + score;
stext.setTextFormat(sform);
stext.width = 700;
addChild(stext);
}
function moveRock(e:TimerEvent):void {
rock.x -= speed;
if (rock.x <= 0) {
rock.x = 850;
rock.y = Math.random() * 600;
}
}
}
}
答案 0 :(得分:0)
再次添加同一个摇滚乐,你不会得到另一块摇滚乐。它只会移动到显示列表的顶部。此外,您需要跟踪岩石的每个实例,以便稍后可以对它们进行碰撞检测。
而不是var rock:Bitmap = new rockClass();使用向量,并在构造函数中初始化它。
var rocks:Vector.<Bitmap>;
public function Main():void
{
rocks = new Vector.<Bitmap>();
rocks.push(new RockClass());
rocks.push(new RockClass());
rocks.push(new RockClass());
if (stage){
init();
}else{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
使用循环添加它们,在更改岩石数量时可以正常工作
//inside init()
for(var i:int=0; i< rocks.length; i++){
addchild(rocks[i]);
}
同样的循环也应该用于碰撞检测。