【发布时间】:2013-07-18 22:51:52
【问题描述】:
我确信有一些愚蠢的错误瞒着我,但我已经做了 2 个多小时了,似乎找不到问题所在。有人请帮我确定我做错了什么,错误在标题中,以下是我的相关代码。
更多有用的细节:
确切的错误如下:
TypeError:错误 #1006:addToValue 不是函数。 在 SimpleMenuMain/onTick() 在 flash.utils::Timer/_timerDispatch() 在 flash.utils::Timer/tick()
游戏运行,但不计分,我要计分。我已经三次检查了我所有的实例名称是否正确,我真的看不出我在那里所做的有什么问题。可能我以某种方式错误地引用了它们,但是代码中使用的所有名称都在我的符号和文本中的对象上......等等。
非常感谢您抽出宝贵的时间,这真的让我很生气。
P.S.---我正在学习一个教程,所以如果我做了一些不必要的事情,请不要怪我!尽管除了帮助我回答这个难题之外,其他 cmets 也很受欢迎 =)。
文档类
package
{
import flash.display.MovieClip;
public class SMGDocClass extends MovieClip
{
public var playScreen:SimpleMenuMain;
public var titleScreen:TitleScreen;
public var gameOver:GameOver;
public function SMGDocClass()
{
titleScreen = new TitleScreen();
titleScreen.addEventListener(NavigationEvent.START,onRequestStart,false,0,true);
titleScreen.x = 0;
titleScreen.y = 0;
addChild(titleScreen);
}
public function onStickman1Death(stickman1Event:Stickman1Event):void
{
var finalScore:Number = playScreen.getFinalScore();
var finalClockTime:Number = playScreen.getFinalClockTime();
gameOver = new GameOver();
gameOver.addEventListener(NavigationEvent.RESTART,onRequestRestart,false,0,true);
gameOver.addEventListener(NavigationEvent.MAINMENU,onRequestMainMenu,false,0,true);
gameOver.x = 5;
gameOver.y = 6;
gameOver.setFinalScore( finalScore );
gameOver.setFinalClockTime( finalClockTime );
addChild(gameOver);
playScreen = null;
}
public function onRequestStart( navigationEvent:NavigationEvent ):void
{
playScreen = new SimpleMenuMain();
playScreen.addEventListener( Stickman1Event.DEAD, onStickman1Death,false,0,true );
playScreen.x = 0;
playScreen.y = 0;
addChild( playScreen );
titleScreen = null;
}
public function restartGame():void
{
playScreen = new SimpleMenuMain;
playScreen.addEventListener(Stickman1Event.DEAD, onStickman1Death,false,0,true);
playScreen.x = 0;
playScreen.y = 0;
addChild(playScreen);
gameOver = null;
}
public function onRequestRestart(navigationEvent:NavigationEvent):void
{
restartGame();
}
public function onRequestMainMenu( navigationEvent:NavigationEvent):void
{
titleScreen = new TitleScreen();
titleScreen.addEventListener(NavigationEvent.START,onRequestStart,false,0,true);
titleScreen.x = 0;
titleScreen.y = 0;
addChild(titleScreen);
removeChild(gameOver);
gameOver = null;
}
}
}
playScreen 类
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class SimpleMenuMain extends MovieClip {
public var army1:Array;
public var enemy1:Enemy1;
public var gameTimer:Timer;
public var stickman1:Stickman1;
public function SimpleMenuMain() {
army1 = new Array();
var newEnemy1 = new Enemy1( 100, -15 );
army1.push(newEnemy1);
addChild(newEnemy1);
stickman1 = new Stickman1();
addChild(stickman1);
stickman1.x = mouseX;
stickman1.y = mouseY;
gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick, false, 0, true);
gameTimer.start();
}
public function onTick(timerEvent:TimerEvent):void
{
gameClock.addToValue( 25 );
if ( Math.random() < 0.1 )
{
var randomX:Number = Math.random() * 400;
var newEnemy1:Enemy1 = new Enemy1( randomX, -15 );
army1.push( newEnemy1 );
addChild( newEnemy1 );
gameScore.addToValue( 10 );
}
stickman1.x = mouseX;
stickman1.y = mouseY;
for each (var enemy1:Enemy1 in army1)
{
enemy1.moveDown();
if (stickman1.hitTestObject(enemy1))
{
gameTimer.stop();
dispatchEvent(new Stickman1Event(Stickman1Event.DEAD));
}
}
}
public function getFinalScore():Number
{
return gameScore.currentValue;
}
public function getFinalClockTime():Number
{
return gameClock.currentValue;
}
}
}
计数器类
package
{
import flash.display.MovieClip;
public class Counter extends MovieClip
{
public var currentValue:Number;
public function Counter()
{
reset();
}
public function addToValue( amountToAdd:Number ):void
{
currentValue = currentValue + amountToAdd;
updateDisplay();
}
public function reset():void
{
currentValue = 0;
updateDisplay();
}
public function updateDisplay():void
{
}
}
}
Stickman1Event 类
package
{
import flash.events.Event;
public class Stickman1Event extends Event
{
public static const DEAD:String = "dead";
public function Stickman1Event( type:String, bubbles:Boolean = false, cancelable:Boolean = false )
{
super( type, bubbles, cancelable );
}
public override function clone():Event
{
return new Stickman1Event( type, bubbles, cancelable );
}
public override function toString():String
{
return formatToString( "Stickman1Event", "type", "bubbles", "cancelable", "eventPhase" );
}
}
}
导航事件类
package
{
import flash.events.Event;
public class NavigationEvent extends Event
{
public static const RESTART:String = "restart";
public static const START:String = "start";
public static const MAINMENU:String = "mainmenu";
public function NavigationEvent( type:String, bubbles:Boolean = false, cancelable:Boolean = false )
{
super( type, bubbles, cancelable );
}
public override function clone():Event
{
return new NavigationEvent( type, bubbles, cancelable );
}
public override function toString():String
{
return formatToString( "NavigationEvent", "type", "bubbles", "cancelable", "eventPhase" );
}
}
}
成绩等级
package
{
import flash.text.TextField;
public class Score extends Counter
{
public function Score()
{
super();
}
override public function updateDisplay():void
{
super.updateDisplay();
scoreDisplay.text = currentValue.toString();
}
}
}
【问题讨论】:
-
除非我完全失明,否则您永远不会定义或实例化
gameScore和gameClock,这两个您试图调用addToValue()的对象。不知道为什么这不会引发 null-ref 错误,但这绝对是问题所在。这些对象不存在,因此不能对它们使用该方法。 -
这些都被定义为我的主时间线上的实例......这就是为什么它没有抛出一个完整的错误我认为......只要我给了它们实例名称我认为我应该能够像那样引用它们就好了?
-
它们是
Counter的实例吗? Counter 具有addToValue()方法,因此他们需要扩展该类才能访问该方法。