大家好!我是杰里米,一名大学生。我正在制作一个有故事和最多3级的射击游戏(这是我们在2天内到期的决赛)。我知道我必须知道学校的这个东西bcoz,但说实话,我不。而且我对动作3非常不满意所以我真的需要你的帮助。我知道这个问题一直在网上。但我仍然不知道如何解决错误代码1009和2007 ..
所以这是我的第4帧代码:
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.events.Event;
stop();
var container:MovieClip = new MovieClip();
addChild(container);
//move the spaceship using our mouse
function controlSpaceShip(event:Event):void
{
shipMC.x = stage.mouseX;
Mouse.hide();
}
stop();
var score:int = 0;
stage.addEventListener(Event.ENTER_FRAME, controlSpaceShip);
//shoot bullets when we left click on the mouse
function shootBullet(event:MouseEvent):void
{
var thebullet:MovieClip = new bullet(); //calls the bullet symbol from the library
//positions the bullet at the position of your ship
thebullet.x = shipMC.x + 30;
thebullet.y = shipMC.y + 20;
//we add the bullet onto the stage
container.addChild(thebullet);
}
stage.addEventListener(MouseEvent.CLICK, shootBullet);
//when bullet hits the enemy1
function hitEnemy(event:Event):void {
if (container.hitTestObject(enemy1)) {
enemy1.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy);
//when bullet hits the enemy2
function hitEnemy2(event:Event):void {
if (container.hitTestObject(enemy2)) {
enemy2.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy2);
//when bullet hits the enemy3
function hitEnemy3(event:Event):void {
if (container.hitTestObject(enemy3)) {
enemy3.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy3);
//when bullet hits the enemy4
function hitEnemy4(event:Event):void {
if (container.hitTestObject(enemy4)) {
enemy4.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy4);
//when bullet hits the enemy5
function hitEnemy5(event:Event):void {
if (container.hitTestObject(enemy5)) {
enemy5.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy5);
//when bullet hits the enemy6
function hitEnemy6(event:Event):void {
if (container.hitTestObject(enemy6)) {
enemy6.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
if (txtScore.text == (150).toString())
{
gotoAndStop(5);
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy6);
//move the enemies vertically
function moveEnemies(event:Event):void {
//ENEMY 1
enemy1.y += 8; //increment the y location of my enemy 6 pixels
if (enemy1.y > 600) { //if my enemy is outside the stage
enemy1.y = 0; //place it back on the stage
enemy1.x = Math.random() * 550; //randomize its horizontal location
}
//ENEMY 2
enemy2.y += 4; //increment the y location of my enemy 3 pixels
if (enemy2.y > 600) { //if my enemy is outside the stage
enemy2.y = 0; //place it back on the stage
enemy2.x = Math.random() * 550; //randomize its horizontal location
}
//ENEMY 3
enemy3.y += 3; //increment the y location of my enemy 3 pixels
if (enemy3.y > 600) { //if my enemy is outside the stage
enemy3.y = 0; //place it back on the stage
enemy3.x = Math.random() * 550; //randomize its horizontal location
}
//ENEMY 4
enemy4.y += 4; //increment the y location of my enemy 3 pixels
if (enemy4.y > 600) { //if my enemy is outside the stage
enemy4.y = 0; //place it back on the stage
enemy4.x = Math.random() * 550; //randomize its horizontal location
}
//ENEMY 5
enemy5.y += 4; //increment the y location of my enemy 3 pixels
if (enemy5.y > 600) { //if my enemy is outside the stage
enemy5.y = 0; //place it back on the stage
enemy5.x = Math.random() * 550; //randomize its horizontal location
}
//ENEMY 6
enemy6.y += 4; //increment the y location of my enemy 3 pixels
if (enemy6.y > 600) { //if my enemy is outside the stage
enemy6.y = 0; //place it back on the stage
enemy6.x = Math.random() * 550; //randomize its horizontal location
}
}
stage.addEventListener(Event.ENTER_FRAME, moveEnemies);
所以在杀死所有6个敌人并且得分至少150之后,我希望框架跳到第5帧(成功),而第5帧有按钮点击进入第6帧(故事并转到第2级)..
这是继续前进和下去的错误..
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/moveEnemies()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/controlSpaceShip()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy2()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy3()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy4()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy5()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy6()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/moveEnemies()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/controlSpaceShip()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy2()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy3()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy4()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy5()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy6()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/moveEnemies()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/controlSpaceShip()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy2()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy3()
我希望有人帮助我,因为我不知道该怎么办。我没有人帮助我。我的同学不平易近人,友好。这将在2天内提交。所以我不想失败。我希望有人......有人帮助我完成比赛。
您可以在Actionscript3 shooter game查看我的文件 谢谢。
答案 0 :(得分:0)
您的问题是您添加了所有这些ENTER_FRAME
侦听器,但在转到下一帧之前不会删除它们。所以他们一直在奔跑,他们试图引用你所有的敌人和容器,即使这些东西在你换帧时都被卸下了。
要解决此问题,您需要先删除所有这些侦听器,然后再转到第5帧。
我建议创建一个名为checkGameComplete
的函数或类似的函数:
function checkGameComplete():void {
if (txtScore.text == (150).toString()){
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy6); //repeat for ALL enter frame listeners
//rest of the removeEventListeners
gotoAndStop(5);
}
}
然后在每次hitEnemy检查时调用checkGameComplete()
。
顺便说一下,通过制作一个命中敌人方法,一个移动方法和一个输入框架监听器来减少你的代码冗余:
stop();
var score:int = 0;
var container:MovieClip = new MovieClip();
addChild(container);
stage.addEventListener(Event.ENTER_FRAME, gameLoop); //just the one ENTER FRAME handler
Mouse.hide(); //you only need to call this once, instead of every bullet fire
stage.addEventListener(MouseEvent.CLICK, shootBullet);
function shootBullet(event:MouseEvent):void
{
var thebullet:MovieClip = new bullet(); //calls the bullet symbol from the library
//positions the bullet at the position of your ship
thebullet.x = shipMC.x + 30;
thebullet.y = shipMC.y + 20;
//we add the bullet onto the stage
container.addChild(thebullet);
}
function gameLoop(event:Event):void {
if(!shipMC) return;
shipMC.x = stage.mouseX;
moveEnemy(enemy1, 8);
moveEnemy(enemy2, 4);
moveEnemy(enemy3, 3);
moveEnemy(enemy4, 4);
moveEnemy(enemy5, 4);
moveEnemy(enemy6, 4);
}
function hitEnemyCheck(enemy):void {
if (container.hitTestObject(enemy)) {
//enemy.visible = false; //instead of hiding it, move it back up
enemy.y = -100;
score = score + 5;
txtScore.text = (score).toString();
checkGameComplete();
}
}
function moveEnemy(enemy, amount):void {
enemy.y += amount; //increment the y location of my enemy
if (enemy.y > 600) { //if my enemy is outside the stage
enemy.y = 0; //place it back on the stage
enemy.x = Math.random() * 550; //randomize its horizontal location
}
hitEnemyCheck(enemy);
}
function checkGameComplete():void {
if (txtScore.text == (150).toString()){
stage.removeEventListener(Event.ENTER_FRAME, gameLoop);
stage.removeEventListener(MouseEvent.CLICK, shootBullet); //clean up listeners
removeChild(container); //get rid of this container you created
Mouse.show(); //show the mouse again
gotoAndStop(5);
}
}
我也注意到,在你的第5帧代码(以及第6帧代码)中,你在next3_btn
点击上调用了错误的函数:
next3_btn.addEventListener(MouseEvent.CLICK,next2Click); // <-- your calling next2Click instead of next3Click
应该是:
next3_btn.addEventListener(MouseEvent.CLICK,next3Click);
答案 1 :(得分:0)
在hitEnemy6
函数和gotoAndStop(5)
之前,您应该删除已添加的所有事件侦听器,转到第5帧:
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy)
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy2)
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy3)
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy4)
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy5)
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy6)
stage.removeEventListener(Event.ENTER_FRAME, moveEnemies)
stage.removeEventListener(Event.ENTER_FRAME, controlSpaceShip)
stage.removeEventListener(MouseEvent.CLICK, shootBullet)
gotoAndStop(5)
在moveEnemies
函数中,您应该使用简单的enemy
每次验证if
对象是否为空:
if (enemy1) {
enemy1.y += 8
if (enemy1.y > 600){
enemy1.y = 0
enemy1.x = Math.random() * 550
}
}
您应该为所有enemy
个对象(1 - &gt; 6)执行此操作。
我不知道为什么你创建了6个Event.ENTER_FRAME
,每个enemy
对象创建一个!{你可以这样做:
stage.addEventListener(Event.ENTER_FRAME, hitEnemy)
function hitEnemy(event:Event):void {
if (container.hitTestObject(enemy1)) {
enemy1.alpha = 0
score = score + 5
txtScore.text = (score).toString()
}
if (container.hitTestObject(enemy2)) {
enemy2.alpha = 0
score = score + 5
txtScore.text = (score).toString()
}
// do the same thing for all your enemy objects ...
}
...