stop(); undefined method in as3.0 - actionscript-3

i want to move to the next frame after clicking a specific button. tried putting stop(); method but it says error and the result is just alternating the 2 frames .
here's my code.
//the error says call to a possibly undefined method stop.
//i'm using adobe flash cc.
stop();
public function main ():void {
enter_button.buttonMode = true;
enter_button.addEventListener(MouseEvent.MOUSE_DOWN, checkForm);
player.text = "";
}
public function checkForm (event:MouseEvent):void {
if (player.text != ""){
gotoAndStop(1);
sendForm();
}
else{
player.text = "please enter your name";
}
}

Try MovieClip(root).gotoAndStop(1); - assuming you're trying to change frames on the main timeline and not an object.
Also, it's not clear where you're using this code, (on the timeline, in a class, or in the main .as) but stop(); should be placed in the actions panel of every frame of the timeline / movieClip.

Related

Flash CS3 AS3 Movieclips carrying over to other frames

OK so I am having a weird issue. I have some movieclips on screen, 4 of them, each with the following code (with different instance names of course):
stage.addEventListener(MouseEvent.MOUSE_DOWN,globalMouseDown,false,0,true); //add a global mouse listener
function globalMouseDown(e:Event):void {
//find out if the target is a descendant of this, if not, then something else was clicked.
var parent:DisplayObject = e.target as DisplayObject;
while(parent && parent != stage){
if(parent == this) return;
parent = parent.parent;
}
//something else was clicked that wasn't this, so go to the up state
gotoAndStop(1);
}
stop();
addEventListener(MouseEvent.MOUSE_DOWN, onHs1Press);
addEventListener(MouseEvent.MOUSE_OVER, onHs1Over);
addEventListener(MouseEvent.MOUSE_OUT, onHs1Out);
function onHs1Press(event:MouseEvent):void
{
// toggle between frame 1 and 3 on button press
gotoAndStop(this.currentFrame == 3 ? 1 : 3);
parent.addChild(this)
}
function onHs1Over(event:MouseEvent):void
{
if (currentFrame != 3)
{
gotoAndStop(2);
}
}
function onHs1Out(event:MouseEvent):void
{
// only switch back to UP state if the button is "pressed"
if (currentFrame != 3)
{
gotoAndStop(1);
}
}
Basically it lets you hover your mouse and the movieclip changes and then when you click on it a little pop up window appears until you click the movieclip again to close it.
There is also a button on screen that allows you to move forward or backwards to other frames with this code:
Next.addEventListener(MouseEvent.CLICK,Nclick);
function Nclick(event:MouseEvent):void {
nextFrame();
}
Back.addEventListener(MouseEvent.CLICK,Bclick);
function Bclick(event:MouseEvent):void {
prevFrame();
}
The button code is on the main timeline and the movieclip code is on the movieclip's timeline.
For some reason if you have the movieclip in the DOWN state (with the popup window open) and you click the button to go to the next frame, the movieclip follows onto the next and any other frames instead of just going away.
I have this same code present on other frames and none of the other ones behave this way, it's really weird.
You can even click it still when its on the other frames and bring up the popup window where the movieclip and code aren't even present.
What's going on with it?
I tried testing this, and could reproduce your issue. If you add a movieclip to the stage in FlashPro, after changing it's index or parentage, it will from that point on be treated like an object created from code and the timeline will ignore it and even create another instance of it on a frame where it is created.
You'll have to manually remove the buttons from the display list.
function Nclick(event:MouseEvent):void {
nextFrame();
removeBtns();
}
function Bclick(event:MouseEvent):void {
prevFrame();
removeBtns();
}
function removeBtns():void {
if(currentFrame != 2){ //whatever the frame of your buttons is
if(btn1 && btn1.parent) removeChild(btn1); //btn1 being whatever your button instnace name is
if(btn2 && btn2.parent) removeChild(btn2); //repeat for all buttons
}
}
OR If you'd prefer to have encapsulated code, instead of the above, put this on your button class/timeline:
var myFrame:int = MovieClip(parent).currentFrame;
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
this.addEventListener(Event.REMOVED_FROM_STAGE,removedHandler);
function enterFrameHandler(e:Event):void {
if(MovieClip(parent).currentFrame != myFrame){
parent.removeChild(this);
}
}
function removedHandler(e:Event):void {
this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
this.removeEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
}

play/pause button for flash as3 gallery

I have a slideshow with 4 pictures and runs on a timer. I have a movie clip called play_mc. Inside the movie clip is a button with an instance name of play_btn on keyframe 1. Then on keyframe 2 there is another button with an instance name pause_btn. On the AS3 layer, I have this code:
stop();
play_btn.addEventListener(MouseEvent.CLICK, goToPause);
function goToPause(Event:MouseEvent){
gotoAndStop(2);
}
pause_btn.addEventListener(MouseEvent.CLICK, goToPlay);
function goToPlay(Event:MouseEvent){
gotoAndStop(1);
}
On the main stage on the as3 layer, I have this code (this is not all of the code - all other code works without the play_mc movie clip)
myTimer.addEventListener(TimerEvent.TIMER, autoAdvance);
function autoAdvance(event:TimerEvent){
if(imageNumber<totalImages){
imageNumber++;
}
else(imageNumber = 1);
reload();
}
function reload(){
removeChild(myLoader);
myRequest = new URLRequest(imageNumber + ".jpg");
myLoader.load(myRequest);
addChildAt(myLoader, 1);
}
play_mc.addEventListener(MouseEvent.MOUSE_DOWN, stopTimer);
function stopTimer(event:Event){
myTimer.stop();
}
play_mc.addEventListener(MouseEvent.MOUSE_DOWN, resumeTimer);
function resumeTimer(event:Event){
myTimer.start();
}
I get an error saying:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at gallery_fla::play_Mc_3/frame1()
Basically when I click on the Play button the slideshow starts and the text changes to "Pause" but when I click again, the slideshow does not pause and the text won't change back to "Play".
Anyone have an idea how to help me out here, please??
You've created the pause button eventListener on the frame where the button doesn't exist.
Try placing this
pause_btn.addEventListener(MouseEvent.CLICK, goToPlay);
function goToPlay(Event:MouseEvent){
gotoAndStop(1);
}
on frame two which has the pause button on it.
edit:
instead of creating two listeners on play_mc, just create one and then base the actions on a switch i.e.
var _playToggle:Boolean = true;
play_mc.addEventListener(MouseEvent.MOUSE_DOWN, switchTimer);
function switchTimer(event:Event){
if(_playToggle){
myTimer.start();
}else{
myTimer.stop()
}
_playToggle = !_playToggle;
}

want to change the code actionscript2 to actionscript3?

i am newbie to flash.i need to change the below actionscript code to actionscript 3.0 code.
i am currently working on drag and drop. so i want to duplicate the movieclip while dragging i found the code on internet but it is actionscript 2.0 so please convert it to as3. the box is a instance name of a movieclip.
the code blocks are:
var num:Number = 0
box.onPress = function(){
num++
duplicateMovieClip(box ,"box"+num, _root.getNextHighestDepth())
_root["box"+num].startDrag();
}
box.onReleaseOutside = function(){
trace(_root["box"+num])
stopDrag();
}
If you dont want to use seperate .as file, follow this steps:
1- assign AS linkage to box movieClip (in library panel):
2- Select frame 1 on the timeline, and paste this code in the Actions panel:
var boxes:Array=[];
//var box:Box=new Box();
//addChild(box);
box.addEventListener(MouseEvent.MOUSE_DOWN,generateBox);
function generateBox(e:MouseEvent):void{
var newBox:Box=new Box();
newBox.x = e.target.x;
newBox.y = e.target.y;
newBox.startDrag();
newBox.addEventListener(MouseEvent.MOUSE_UP,stopD);
newBox.addEventListener(MouseEvent.MOUSE_DOWN,startD);
boxes.push(newBox);
addChild(newBox);
}
function startD(e:MouseEvent):void{
e.target.startDrag();
}
function stopD(e:MouseEvent):void{
e.target.stopDrag();
}
Unfortunately, there's no duplicateMovieClip analog in AS3, so you'll have to create a Class for your box movieClip template. Let's say it will be called BoxTemplate. (You can google how to create Classes for your library object). Add a Class with this name and add this code (event subscription in the constructor and a private event listener). You'll get something like this:
package
{
public class BoxTemplate
{
public function BoxTemplate()
{
addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
}
private function onMouseUp(e:MouseEvent):void
{
stopDrag();
}
}
Leave your present instance of this symbol on the stage. This is your code in the frame:
import flash.event.MouseEvent
box.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
var newBox:BoxTemplate = new BoxTemplate();
newBox.x = e.target.x;
newBox.y = e.target.y;
addChild(newBox);
newBox.startDrag();
}
It will allow you to infinitely clone your boxes. Of course, you can add all of them in the array to keep the references.

How to delay when my actionscript 3 code runs?

I am creating a drag and drop game that I followed through a Lynda tut. I kept getting an error for my game that I created because I noticed (after weeks of reviewing the code and having other people look at it to figure out what was wrong) that the tutorial that I followed did everything on frame one but I was making my game start at frame 3. So if I start my game at frame 1, it works perfectly and I wont get these errors:
This occurs when I test the movie, once I click continue I am able to see the movie -
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at simpleSpring()[simpleSpring.as:21]
And this occurs when I drag my object -
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at DragDrop/drop()[DragDrop.as:33]
Since I know these errors won't appear unless I begin the game at frame 1, I want to know what code I can place so that I can begin the game at frames that are past the first frame.
The following is the code for DragDrop.as
package
{
import flash.display.*;
import flash.events.*;
public class DragDrop extends Sprite
{
var origX:Number;
var origY:Number;
var target:DisplayObject;
public function DragDrop()
{
// constructor code
origX = x;
origY = y;
addEventListener(MouseEvent.MOUSE_DOWN, drag);
buttonMode = true;
}
function drag(evt:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
startDrag();
parent.addChild(this);
}
function drop(evt:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
stopDrag();
if(hitTestObject(target))
{
visible = false;
target.alpha = 1;
Object(parent).match();
}
x = origX;
y = origY;
}
}
}
And here is the simpleSpring.as
package
{
import flash.display.*;
import flash.events.*;
public class simpleSpring extends MovieClip
{
var dragdrops:Array;
public function simpleSpring()
{
// constructor code
dragdrops = [ladyone,ladytwo,ladythree,ladyfour,ladyfive,ladysix];
var currentObject:DragDrop;
for(var i:uint = 0; i < dragdrops.length; i++)
{
currentObject = dragdrops[i];
currentObject.target = getChildByName(currentObject.name + "_target");
}
}
public function match():void
{
}
}
}
I tried adding the code to a actions layer in the game document but that also does not seem to work correctly.
I am such a newbie when it comes to publishing things. I noticed I can add multiple swf files when I publish for android. This solves my issue of not being able to code at the beginning frame. If I have this game in a separate flash file saved in the same folder with the title movie calling to it with actionscript, the code will work and I am able to publish the whole thing as one file. Thanks again for those that tried to help me figure this out!

Action Script 3: Problems with gotoAndStop() after a Movie Clip

Upon the click of a button, an animation starts. Then the program directs you to a certain frame when the animation is done.
Is this possible?
So this is what I've got so far: a Movie Clip movQuizIntro and a Button btnBond in Frame 1.
stop()
movQuizIntro.stop()
btnBond.addEventListener(MouseEvent.CLICK, BondQuiz)
btnReg.addEventListener(MouseEvent.CLICK, Registrering)
function BondQuiz (evt:MouseEvent)
{
if (currentFrame == 1)
{
movQuizIntro.alpha = 1
movQuizIntro.play()
}
}
What is the code and proper syntax you need to write in order to go to frame 2 after the animation is done?
`
stop();
movQuizIntro.stop();
int frameCounter=0;
btnBond.addEventListener(MouseEvent.CLICK, BondQuiz);
btnReg.addEventListener(MouseEvent.CLICK, Registrering);
function BondQuiz (evt:MouseEvent)
{
if (currentFrame == 1)
{
movQuizIntro.alpha = 1
movQuizIntro.play()
movQuizIntro.addEventListener(EventType.ENTER_FRAME, onEnterFrame);
}
}
// event handler function, runs every enter frame
private function onEnterFrame(event:Event):Void
{
frameCounter++;
if(frameCounter > movQuizIntro.totalFrames)
{
//Place code here because you know the MovieClip finished playings
//Go to desired frame
}
}
`
I wrote this code outside of an editor nor did I get to compile, so the gist is there and may have some minor errors.
NOTE:This is just a quick way of doing this. If you want something more reusable/cleaner then you would want to consider subclassing or alternate Object Oriented tricks.
In button event handler:
function onClick(e:MouseEvent):void{
ANIMATION_MC.addEventListener(Event.EXIT_FRAME, onFromeExit);
}
function onFrameExit(e:Event):void {
if (ANIMATION_MC.currentFrame == SOME_FRAME) {
ANIMATION_MC.removeEventListener(Event.EXIT_FRAME, onFromeExit);
TARGET.gotoAndPlay(NEW_FRAME);
}
}
And you can just use addFrameScript on ANIMATION_MC too.