RR_QQ
07-02-2008, 06:31 PM
Ok so Im trying to learn animation in AS3 and I am using Flex Builder 3 to create my Flash animations. I am learning from the book Foundation ActionScript 3.0 Animation, Making Things Move. One thing I have not been able to figure out is an error I get when I try to use the 'stage' property in a sprite. Here is a sample class I'm trying to use:
package animation
{
import Shapes.Ball;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.events.Event;
public class SecondAnimation extends Sprite
{
public function SecondAnimation()
{
init();
}
private function init():void
{
graphics.lineStyle(1);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseDown(event:MouseEvent):void
{
graphics.moveTo(mouseX, mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onMouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onMouseMove(event:MouseEvent):void
{
graphics.lineTo(mouseX, mouseY);
}
}
}
That gives me this runtime error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at animation::SecondAnimation/init()[C:\Documents and Settings\user1\My Documents\Flex Builder 3\AnimationTest1\src\animation\SecondAnimation.as: 20]
at animation::SecondAnimation()[C:\Documents and Settings\user1\My Documents\Flex Builder 3\AnimationTest1\src\animation\SecondAnimation.as: 14]
at BallMotion/initApplication()[C:\Documents and Settings\user1\My Documents\Flex Builder 3\AnimationTest1\src\BallMotionMain.as:23]
at BallMotion/___BallMotion_Application1_creationComplete()[C:\Documents and Settings\user1\My Documents\Flex Builder 3\AnimationTest1\src\BallMotion.mxml:3]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\UIComponent.as:9051]
at mx.core::UIComponent/set initialized()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\UIComponent.as:1167]
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ managers\LayoutManager.as:698]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at mx.core::UIComponent/callLaterDispatcher2()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\UIComponent.as:8460]
at mx.core::UIComponent/callLaterDispatcher()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\UIComponent.as:8403]
I have gotten around the problem by doing things a bit differently but I think it's time I addressed this problem. Does anyone know why I cannot get this to work?
Here is my MXML and Main.as file:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="initApplication();">
<mx:Script source="Main.as"/>
<mx:Canvas id="animationStage">
</mx:Canvas>
<mx:Button x="48" y="237" label="Restart" click="onRestartClickHandler();"/>
</mx:Application>
Main.as:
import animation.*;
import mx.core.UIComponent;
public var ballanimation:BallMotion;
public var secondAnimation:SecondAnimation;
public function initApplication():void
{
// create a new sprite object
/*ballanimation = new BallMotion();
// need to create a UI wrapper to be able to add the sprite to the canvas
var uiWrapper:UIComponent = new UIComponent();
uiWrapper.addChild(ballanimation);
// add the child
animationStage.addChild(uiWrapper);
*/
// create a new sprite object
secondAnimation = new SecondAnimation();
// need to create a UI wrapper to be able to add the sprite to the canvas
var uiWrapper:UIComponent = new UIComponent();
uiWrapper.addChild(secondAnimation);
// add the child
animationStage.addChild(uiWrapper);
}
public function onRestartClickHandler():void
{
//secondAnimation.restart();
}
Any help is appreciated!
package animation
{
import Shapes.Ball;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.events.Event;
public class SecondAnimation extends Sprite
{
public function SecondAnimation()
{
init();
}
private function init():void
{
graphics.lineStyle(1);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseDown(event:MouseEvent):void
{
graphics.moveTo(mouseX, mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onMouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
private function onMouseMove(event:MouseEvent):void
{
graphics.lineTo(mouseX, mouseY);
}
}
}
That gives me this runtime error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at animation::SecondAnimation/init()[C:\Documents and Settings\user1\My Documents\Flex Builder 3\AnimationTest1\src\animation\SecondAnimation.as: 20]
at animation::SecondAnimation()[C:\Documents and Settings\user1\My Documents\Flex Builder 3\AnimationTest1\src\animation\SecondAnimation.as: 14]
at BallMotion/initApplication()[C:\Documents and Settings\user1\My Documents\Flex Builder 3\AnimationTest1\src\BallMotionMain.as:23]
at BallMotion/___BallMotion_Application1_creationComplete()[C:\Documents and Settings\user1\My Documents\Flex Builder 3\AnimationTest1\src\BallMotion.mxml:3]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\UIComponent.as:9051]
at mx.core::UIComponent/set initialized()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\UIComponent.as:1167]
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ managers\LayoutManager.as:698]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at mx.core::UIComponent/callLaterDispatcher2()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\UIComponent.as:8460]
at mx.core::UIComponent/callLaterDispatcher()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\ core\UIComponent.as:8403]
I have gotten around the problem by doing things a bit differently but I think it's time I addressed this problem. Does anyone know why I cannot get this to work?
Here is my MXML and Main.as file:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="initApplication();">
<mx:Script source="Main.as"/>
<mx:Canvas id="animationStage">
</mx:Canvas>
<mx:Button x="48" y="237" label="Restart" click="onRestartClickHandler();"/>
</mx:Application>
Main.as:
import animation.*;
import mx.core.UIComponent;
public var ballanimation:BallMotion;
public var secondAnimation:SecondAnimation;
public function initApplication():void
{
// create a new sprite object
/*ballanimation = new BallMotion();
// need to create a UI wrapper to be able to add the sprite to the canvas
var uiWrapper:UIComponent = new UIComponent();
uiWrapper.addChild(ballanimation);
// add the child
animationStage.addChild(uiWrapper);
*/
// create a new sprite object
secondAnimation = new SecondAnimation();
// need to create a UI wrapper to be able to add the sprite to the canvas
var uiWrapper:UIComponent = new UIComponent();
uiWrapper.addChild(secondAnimation);
// add the child
animationStage.addChild(uiWrapper);
}
public function onRestartClickHandler():void
{
//secondAnimation.restart();
}
Any help is appreciated!