这是我的整个调试错误:
Car initiated: [object MovieClip]
Wheel initiated: [object MovieClip]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.George.MegaAmazingApp.Components::Wheel/clickHandler()[C:\Documents and Settings\reithg\My Documents\Classes\com\George\MegaAmazingApp\Components\Wheel.as:24]
[UnloadSWF] GeorgesMegaAmazingApp.swf
Test Movie terminated.
Car.as:
package com.George.MegaAmazingApp.Components
{
import flash.display.MovieClip;
public class Car extends MovieClip
{
public var carObj:MovieClip;
public function Car(carObj:MovieClip)
{
trace("Car initiated: " + carObj)
this.carObj = carObj;
}
public function Accelerate(speed:Number)
{
carObj.y -= speed;
}
}
}
Wheel.as
package com.George.MegaAmazingApp.Components
{
import flash.display.Stage;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
public class Wheel extends Car
{
public var rotated:Number;
public var wheelObj:MovieClip;
public function Wheel(wheelObj:MovieClip, carObj:MovieClip)
{
super(carObj);
this.carObj = carObj;
this.wheelObj = wheelObj;
trace("Wheel initiated: " + wheelObj);
wheelObj.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(event:MouseEvent):void
{
stage.addEventListener(Event.ENTER_FRAME, super.Accelerate(2));
}
}
}
和我的SWF启动它:
import com.George.MegaAmazingApp.Components.*;
var wheelObj:Wheel = new Wheel(this.wheel,this.car);
任何人都知道什么是错的?
答案 0 :(得分:0)
编辑:stage
属性设置为null
,直到将对象(或其父项之一)添加到舞台中。因此要么使用wheelObj
的舞台,要么首先将轮子添加到舞台上。如果您不想显示Wheel实例本身,则无需扩展MovieClip
。
private function clickHandler(event:MouseEvent):void
{
// event.currentTarget is the wheelObject here
event.currentTarget.stage.addEventListener(Event.ENTER_FRAME, Accelerate);
}
无论如何,你不需要在舞台上添加听众,将它们添加到方向盘本身就可以了:
private function clickHandler(event:MouseEvent):void
{
event.currentTarget.addEventListener(Event.ENTER_FRAME, Accelerate);
}
请注意,您必须提供函数作为参数,而不是函数调用。因此,您还需要更改accelerate
功能。
private var speed:int = 2;
public function accelerate(e:Event) // best practice:use camelCase function names
{
carObj.y -= speed;
}
答案 1 :(得分:0)
测试舞台是否存在(曲目(舞台)),(MovieClip()。舞台应该是有效的,如果你的MovieClip在displayylist上并且舞台存在)。你对super.Accelerate(2)的调用也是错误的。如果您延长汽车,您可以拨打加速(2)。
如果您的MovieClip再次出现在displayylist上,您可以在不访问舞台的情况下收听ENTER_FRAME。 this.addEventListener(Event.ENTER_FRAME,handleEnterFrame)。
答案 2 :(得分:0)
另外,我不会添加一个ENTER_FRAME eventlistener来响应点击。
但如果你这样做,请确保你不添加多个监听器:
private function clickHandler(event:MouseEvent):void
{
if(!hasEventListener(Event.ENTER_FRAME))
{
addEventListener(Event.ENTER_FRAME, Accelerate);
}
}
作为旁注,加速功能现在没有任何效果。它正在以恒定的速度移动汽车。该操作更像是Run()或类似的东西。但这与你的实际问题无关,所以...... sidenote