我试图为我的游戏添加一些if语句,但它已停止工作。我一直收到上述错误。这是我的代码:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var gravity:Number = 0.7;
private var thrust:Number = 1.00;
private var yspeed:Number = 0;
private var xspeed:Number = 10;
private var distance:Number = 0;
private var smokeInterval:Number = 10;
private var framesPassed:Number = 0;
private var fuelFrequency:Number = 10;
private var gasoline:Number = 500;
private var rockFrequency:Number = 150;
private var engines:Boolean = false;
private var gameend:Boolean = false;
private var gameStarted:Boolean = false;
private var ship:Ship=new Ship();
private var score:Score=new Score();
private var fuelbar:Fuelbar=new Fuelbar();
private var newscrollbar:newScrollBar=new newScrollBar();
private var bg:Bg=new Bg();
private var getready:getReady=new getReady();
private var gameover:gameOver=new gameOver();
private var rockCanvas:Sprite=new Sprite();
private var fuelCanvas:Sprite=new Sprite();
private var smokeCanvas:Sprite=new Sprite();
private var fuelVector:Vector.<Fuel>=new Vector.<Fuel>();
private var rockVector:Vector.<Rock>=new Vector.<Rock>();
private var smokeVector:Vector.<Smoke>=new Vector.<Smoke>();
public function Main()
{
addChild(ship);
ship.x = 120;
ship.y = 400;
addChild(score);
addChild(fuelbar);
addChild(newscrollbar);
addChild(bg);
addChild(rockCanvas);
addChild(fuelCanvas);
addChild(smokeCanvas);
addChild(getready);
addEventListener(Event.ENTER_FRAME,update);
stage.addEventListener(MouseEvent.MOUSE_DOWN,engineOn);
stage.addEventListener(MouseEvent.MOUSE_UP,engineOff);
}
if (gameend==false)
{
private function engineOn(e:MouseEvent):void
{
engines = true;
framesPassed = smokeInterval;
gameStarted = true;
}
private function engineOff(e:MouseEvent):void
{
engines = false;
smokeInterval = 10;
}
private function update(e:Event):void
{
bg.x = 0;
bg.y = 0;
newscrollbar.x = -550;
newscrollbar.y = 550;
bg.parent.setChildIndex( bg, 0);
fuelbar.width = gasoline / 2;
fuelbar.x = 240;
fuelbar.y = 30;
getready.x = 50;
getready.y = 250;
score.scoreText.text = 0;
score.x = -34;
score.y = 55;
if (gameStarted==true)
{
if (getready.alpha == 0)
{
removeChild(getready);
}
else
{
getready.alpha -= 0.05;
}
if (Math.random() * 1000 < fuelFrequency)
{
var fuel:Fuel=new Fuel();
fuel.y = Math.random() * 600 + 40;
fuel.x = 490;
fuelCanvas.addChild(fuel);
fuelVector.push(fuel);
}
if (Math.random() * 1000 < rockFrequency)
{
var rock:Rock=new Rock();
rock.y = Math.random() * 500 + 40;
rock.x = 520;
rock.rotation = Math.random() * 360;
rockCanvas.addChild(rock);
rockVector.push(rock);
}
distance += xspeed / 10;
score.scoreText.text = distance;
if ((gasoline>0)&&(engines))
{
yspeed -= thrust;
smokeInterval -= 0.25;
gasoline -= 0;
framesPassed++;
if (smokeInterval<framesPassed)
{
var smoke:Smoke=new Smoke();
smoke.x = ship.x;
smoke.y = ship.y;
smokeCanvas.addChild(smoke);
smokeVector.push(smoke);
framesPassed = 0;
smokeInterval -= 0.01;
}
}
yspeed += gravity;
ship.y += yspeed;
angle = Math.atan2(yspeed,xspeed);
ship.rotation = angle * 180 / Math.PI;
for (var i:int=fuelVector.length-1; i>=0; i--)
{
fuelVector[i].x -= xspeed * 1.2;
var dist_x:Number = ship.x + 28 * Math.cos(angle) - fuelVector[i].x;
var dist_y:Number = ship.y + 28 * Math.sin(angle) - fuelVector[i].y;
var dist:Number = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
if (dist<20)
{
gasoline += 100;
fuelCanvas.removeChild(fuelVector[i]);
fuelVector.splice(i,1);
}
else
{
if (fuelVector[i].x < -10)
{
fuelCanvas.removeChild(fuelVector[i]);
fuelVector.splice(i,1);
}
}
}
for (i=rockVector.length-1; i>=0; i--)
{
rockVector[i].x -= xspeed;
if (rockVector[i].x < -25)
{
rockCanvas.removeChild(rockVector[i]);
rockVector.splice(i,1);
}
}
for (i=smokeVector.length-1; i>=0; i--)
{
smokeVector[i].x -= xspeed;
smokeVector[i].width += 0.2;
smokeVector[i].height += 0.2;
smokeVector[i].alpha -= 0.04;
if (smokeVector[i].alpha < 0)
{
smokeCanvas.removeChild(smokeVector[i]);
smokeVector.splice(i,1);
}
}
if (ship.y<0 || newscrollbar.hitTestPoint(ship.x+28*Math.cos(angle), ship.y+28*Math.sin(angle), true) || newscrollbar.hitTestPoint(ship.x+8*Math.cos(angle+Math.PI/2), ship.y+8*Math.sin(angle+Math.PI/2), true) || newscrollbar.hitTestPoint(ship.x+8*Math.cos(angle-Math.PI/2), ship.y+8*Math.sin(angle-Math.PI/2), true) || rockCanvas.hitTestPoint(ship.x+28*Math.cos(angle), ship.y+28*Math.sin(angle), true) || rockCanvas.hitTestPoint(ship.x+8*Math.cos(angle+Math.PI/2), ship.y+8*Math.sin(angle+Math.PI/2), true) || rockCanvas.hitTestPoint(ship.x+8*Math.cos(angle-Math.PI/2), ship.y+8*Math.sin(angle-Math.PI/2), true))
{
addChild(gameover);
gameover.y = 240;
gameover.x = 240;
yspeed = 0;
ship.y = 400;
gasoline = 500;
rockVector=new Vector.<Rock>();
removeChild(rockCanvas);
fuelVector=new Vector.<Fuel>();
removeChild(fuelCanvas);
smokeVector=new Vector.<Smoke>();
removeChild(smokeCanvas);
ship.rotation = 0;
gameStarted = false;
gameend = true;
}
}
}
}
}
}
这主要是代码。 Flash文件中没有框架。 这是一个actionscript文件。谢谢你们。
答案 0 :(得分:0)
if (gameend==false)
上面的行(及其块)在构造函数之外,不在任何方法块之内。 它在类块中漂浮,它不应该这样做。
将函数包装到if语句块中肯定不是可行的方法。 当值改变时,它不会使函数停止。
相反,编写一个在游戏结束时进行所有清理工作的功能,即:删除所有使游戏继续进行的清理工具,移除显示对象等。
然后简单地调用该函数。
经验法则:不要将功能嵌套在一起。