timer not working in class actionscript 3 - actionscript-3

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
public class MasterContainer extends MovieClip
{
public var playTimer:Timer = new Timer(1000,16);
public function MasterContainer()
{
// constructor code
t1.setSoundName("drum.mp3");
t2.setSoundName("pluck.mp3");
t3.setSoundName("drum.mp3");
t4.setSoundName("drum.mp3");
t5.setSoundName("drum.mp3");
t6.setSoundName("drum.mp3");
t7.setSoundName("drum.mp3");
t8.setSoundName("drum.mp3");
masterPlay.addEventListener(flash.events.MouseEvent.CLICK, handleMasterPlay);
playTimer.addEventListener(TimerEvent.TIMER, onTick);
}
public function onTick(event:TimerEvent):void
{
t1.playSound();
t2.playSound();
t3.playSound();
t4.playSound();
t5.playSound();
t6.playSound();
t7.playSound();
t8.playSound();
}
private function handleMasterPlay(e:MouseEvent):void
{
trace('bla');
}
}
}
this a chopped version of my class. the error i am getting is:
C:\Users\Mark\Documents\Creative Multimedia\semester 5\Action Script\project\MasterContainer.as, Line 9 1046: Type was not found or was not a compile-time constant: Timer.
C:\Users\Mark\Documents\Creative Multimedia\semester 5\Action Script\project\MasterContainer.as, Line 9 1180: Call to a possibly undefined method Timer.
C:\Users\Mark\Documents\Creative Multimedia\semester 5\Action Script\project\MasterContainer.as, Line 9 1180: Call to a possibly undefined method Timer.
i dont understand this error any help would be appreciated.

import flash.utils.Timer;
Did you miss this?

All errors indicates that the Timer class can't be found - check your import statement it should contain this: import flash.utils.Timer;

Related

AS3 addEventListener not a recognized method in my custom class

I want to have a Class called Commands that I can put different key presses and functions into that will control game states and variables for quick and easy game testing. I'm trying this, but it's not working...
package gameTesting {
import flash.events.KeyboardEvent;
import flash.events.*;
import flash.ui.Keyboard;
import flash.display.*;
import flash.events.EventDispatcher;
public class Commands {
public function Commands() {
addEventListeners();
}
public function addEventListeners():void{
addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
}
public function keyDown(ke:KeyboardEvent):void{
trace("key pressed");
}
}
}
which throws this error:
C:...\Commands.as, Line 15, Column 4 1180: Call to a possibly undefined method addEventListener.
So, I tried having my class extend something that inherits the EventDispatcher methods:
//...
public class Commands extends DisplayObject{
// ...
but I just get this error thrown from my main .as file when trying to instantiate this Class:
ArgumentError: Error #2012: Commands$ class cannot be instantiated.
I also tried throwing the static keyword around just for lols, but no dice.
What am I missing here?
by the way, my reason for doing things this way is just so that I can remove this functionality (so users can't use it) by simply removing the line of code that instantiates this class. I think it's pretty nifty, but if this seems asinine, by all means speak up!
Try to pass stage to Commands,so you can add addEventListener on stage.
import flash.display.Stage;
public class Commands {
public function Commands(stage:Stage) {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
}
public function keyDown(ke:KeyboardEvent):void{
trace("key pressed");
}
}

undefined method gotoAndPlay

I am not sure why every time I reword this code to do the same thing I usually get the error dealing with gotoAndPlay. Does it have to be a Movieclip because when I change it to a MovieClip an error occurs saying it needs to be extended as a Simple Button because that's what it is defined as. basically.
Error Codes:
1180: Call to a possibly undefined method gotoAndPlay.
package
{
import flash.display.SimpleButton;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class search_button extends SimpleButton
{
public function Search_button()
{
addEventListener(MouseEvent.CLICK, clickHandler);
}
function clickHandler(event:MouseEvent):void
{
trace("goto frame 9");
gotoAndPlay("Search");
}
}
}
Replace
gotoAndPlay("Search");
with
(root as MovieClip).gotoAndPlay("Search");
because search_button doesn't have a frame with the "Search" label. The root is a main timeline.

actionscript 3 - error #1009 Cannot access a property or method of a null object reference

I've only recently started using as so sorry for this
as its probably pretty simple. Im basically trying to spawn an AI unit but am getting the error 1009, here is the full error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at code::Init()[D:\FlashGame\code\Init.as:21]
Im trying to use a function from another class which is in another file. Here is the first file.
package code
{
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.text.TextField;
import code.functions.AIManager;
public class Init extends MovieClip
{
private var _AI:AIManager;
private var _player:MovieClip;
public function Init()
{
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
_AI.createAI();
}
public function enterFrameHandler(event:Event):void
{}
}
}
And the second file..
package code.functions
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.DisplayObjectContainer;
public class AIManager extends MovieClip
{
private var _ai:MovieClip;
public function AIManager()
{
createAI();
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function createAI():void
{
_ai = new AI();
_ai.x = stage.stageWidth / 2;
_ai.y = stage.stageHeight / 2;
stage.addChild(_ai);
}
You need to create an instance of a class before you can use it's methods. The exception to that is static methods. In your case you just need to use new AIManager
_AI = new AIManager();
_AI.createAI();

AS3 Error Call to a possibly undefined method for Underline Function

i have a problem with AS3.
I want to call Underline() method (movie clip class) in my code but there is error.
Here is the error:
1180: Call to a possibly undefined method Underline.
Here is the code:
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.filesystem.*;
import flash.data.*;
import flash.errors.*;
import flash.utils.Timer;
var underline:MovieClip = new Underline();
underline.x = tempText.x + tempText.width / 3;
underline.y = tempText.y + tempText.height / 2 + 5;
textContainer.addChild(underline);
This code works on AS2 but doesn't work on AS3
What is the solution?? Please help, this problem drive me crazy"
As GoldRoger said, Underline here is a class, and must inherit the MovieClip class (extend). Also, when you are creating a new class, there must be a constructor function with the EXACT same name as the class.
For example:
public class Underline extends MovieClip
{
public function Underline()
{
//constructor code, initialize here
}
}

AS3 call to a possibly undefined method through a refernce with a static type?

I have been away from actionscript for a long time and not 100% why this is happening, I will simplify the class below:
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
public class OpiaRobot extends MovieClip{
public function OpiaRobot(){ }
public function botAction(e:MouseEvent):void{ }
public function run(e:Event):void{
this.botaction();
}
}
var opiaBot:OpiaRobot = new OpiaRobot();
The call to botaction causes:
call to a possibly undefined method through a refernce with a static type?
Why as it should be an instance? Any help is appreciated.
If that is your exact code, it's probably because you try to use botaction() when you should use botAction(MouseEvent).