undefined method gotoAndPlay - actionscript-3

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.

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");
}
}

How do I refer to MovieClips / variables of the main .fla file from inside a MovieClip class?

package
{
import flash.display.MovieClip;
import flash.events.Event;
public class OtherMc extends MovieClip
{
public function OtherMc()
{
addEventListener(Event.ENTER_FRAME, eframe);
}
private function eframe(event:Event):void
{
if (hitTestObject(MainPacI))
{
trace("All Good");
}
}
}
}
I have a MovieClip instance on the stage of the mail .fla file called MainPacI and when I run the program, I get the error - 1120:Access of undefined property MainPacI. I apologize if the question is stupid, but I really need to know.
Regards,
Dust
Try using the following
stage.MainPacI

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
}
}

How to call an MovieClip on stage AS3

Trying to addChild() inside a movie clip in the stage from one of my classes. The code looks like this
package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class createFlask extends SimpleButton {
public function createFlask() {
addEventListener(MouseEvent.CLICK, createAFlask);
}
private function createAFlask(e:MouseEvent):void
{
var Flask = new flask ;
Flask.x = stage.width/2;
Flask.y = stage.height/2;
stage.experiment.addChild(Flask);
}
}
This gives an error as
Access of possibly undefined property experiment through a reference
with static type flash.display:Stage.
Any solutions?
Just omit "stage".
Instead use
experiment.addChild(Flask);
That will work.

timer not working in class 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;