play/pause button for flash as3 gallery - actionscript-3

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

Related

Actionscript 3: Run function when mouse down is pressed, everyewhere else than on a listener?

I have an event listener for CLICK on a movie clip. It is very difficult to make a transparent listener behind the movie clip, because of how the stage is arranged. Is there a way I can run a function when you click anywhere else but the listener?
fash.addEventListener(MouseEvent.CLICK, fashRun);
See this API
stage.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
if (!fash.hitTestPoint(stage.mouseX, stage.mouseY, true))
{
//todo
}
}

stop(); undefined method in as3.0

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.

eventlistener functions doesnot stop workning

I have made a content where in one frame I have 10 movieclips(5 color pair) equally divided into two columns.I have added three event listener to the stage mousedown, mouseup, mouse move. I have drawn lines from one movieclip to another to match one column movie clip to another column same movie clip.. I added code to timeline but when I go to next frame or previous frame (where there are another acitvities) using next/prev button a warning is showing up :
Cannot access a property or method of a null object reference.
at CL3_Sc_Pat12_SL05_fla::MainTimeline/mMove()
this waring is not showing for mousedown() mouseup().i have used same next and same previous button for 3 frames.and for frame jumping i numbered each frame as frame no 1,2,3.if frameno == 3 goto frame 2 if frameno== 2 goto frame 1 thus it works..frame jumping code is in 1st frame..
Here is my code :
stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mMove);
function mDown(event:MouseEvent):void
{
mouseHolding = true;
clickedX = mouseX;
clickedY = mouseY;
myDrawing.graphics.moveTo(mouseX, mouseY);
Line_draw.graphics.moveTo(mouseX, mouseY);
if (pencil.hitTestObject(box1)) //box of 1st column
{
trace("box1 value is: "+chk_val_1);
}
}
function mUp(MouseEvent):void
{
myDrawing.graphics.lineTo(mouseX, mouseY);
mouseHolding = false;
if (pencil.hitTestObject(hit_box1)) ////box of 2nd column
{
trace(boxes have same color);
Line_draw.graphics.lineTo(mouseX, mouseY);
}
}
function mMove(MouseEvent):void
{
if (mouseHolding && mouseY < 510 )
{
clearTemp();
Line_draw.graphics.lineTo(mouseX, mouseY);
}
}
function clearTemp():void
{
Line_draw.graphics.clear();
Line_draw.graphics.lineStyle(6,0x0066CC,1);
Line_draw.graphics.moveTo(clickedX, clickedY);
}
function nxt_click(event:MouseEvent)
{
gotoAndPlay(3);
}
function prev_click(event:MouseEvent)
{
gotoAndPlay(1);
}
My code is working perfect but I want to know why is this warning coming again and again ?
You need to draw arrows (lines) just in your "anim4" frame so outside this frame you have to disable this function and remove all stage listener created for that, so you can do like this :
function nxt_click(event:MouseEvent)
{
if(){
// your other instructions
}
// your other instructions
else if (my_frame == 4)
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.removeEventListener(MouseEvent.MOUSE_UP, mUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mMove);
gotoAndPlay("anim5");
}
}
And you should do the same thing when exiting the "anim4" frame by pressing the previous button.
Hope that can help.

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

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.