How to solve when a classic tween is not working with stop() at the first second? - actionscript-3

How to solve when the object is not moving with classic tweening motion in action script coding with stop() command? At my timeline, my object is functioning well with the classic tweening motion. But when I pressed ctrl enter, the object is not moving with the motion.
I've tried on 'gotoAndStop' and 'gotoAndPlay' command.
The code below are the first scene coding and there is a button. When press the button, it will go to scene two.
import flash.events.MouseEvent;
stop();
GWbtn.addEventListener(MouseEvent.CLICK, China);
function China(e:MouseEvent):void{
gotoAndPlay(1, 'Scene 2');
}
In scene two, i have create a classic tweening in the timeline for an object and i include stop() command in the coding as below. When ctrl enter, the tweening is not functioning.
import flash.events.Event;
import flash.events.MouseEvent;
stop();
nextbtn1.addEventListener(MouseEvent.CLICK, next1);
function next1(event:MouseEvent):void{
gotoAndPlay(17);
}
I expect the output of my object is moving with classic tweening and the stop() command.

The issue lies with scene 2 frame 1 having a stop(); in your code. Do this to stop the tweening on frame 16:
import flash.events.Event;
import flash.events.MouseEvent;
//stop(); //remove this line
addEventListener(Event.ENTER_FRAME, checkFrame);
function checkFrame(event:Event):void {
if (currentFrame == 16) {
removeEventListener(Event.ENTER_FRAME, checkFrame);
stop();
}
}
nextbtn1.addEventListener(MouseEvent.CLICK, next1);
function next1(event:MouseEvent):void{
nextbtn1.removeEventListener(MouseEvent.CLICK, next1); //also kindly add this, its good practice
removeEventListener(Event.ENTER_FRAME, checkFrame);
gotoAndPlay(17);
}

Related

FLASH AS3 Sound Overlap

Alright, I'm a total noob in flash as3 so this must be very easy to solve I guess. I'm making a soundboard with recorded voices in flash cs6, very simple: 1 frame, ten buttons, each button makes a different sound. The problem is the overlapping of these sounds, so what I need is that when I press one button the other sounds stop playing. anyone please?
See the play() method documentation in the Sound class, it returns a SoundChannel object, which has a stop() method.
So you could do it like that (schematically) :
var currentChannel:SoundChannel;
button1.addEventListener(MouseEvent.CLICK, onButtonClick);
button2.addEventListener(MouseEvent.CLICK, onButtonClick);
button3.addEventListener(MouseEvent.CLICK, onButtonClick);
function onButtonClick(event:MouseEvent):void
{
/* provided you have implemented selectSoundByButton func somewhere */
const sound:Sound = selectSoundByButton(event.currentTarget);
if (currentChannel) {
currentChannel.stop();
}
currentChannel = sound.play();
}
More detailed description:
Let's say you want to create yet another fart button application in flash. That's what you have to do:
Create a button symbol, add it to stage and give it an instance name in properties tab. Let's call it myButton
Add the sound to library with file->import
Export this sound to actionscript. Right click on a sound in library, check "Export for actionscript", "export in frame 1" on "actionscript tab". Fill "Class" input with a desired class name for a sound (e.g. MySound)
Then you have to trigger the sound playback on your button click. So you should put the following code to the first frame of your flash clip:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
var currentChannel:SoundChannel;
const mySound:Sound = new MySound();
function onClick(e:MouseEvent):void {
if (currentChannel) {
currentChannel.stop();
}
currentChannel = mySound.play();
}
myButton.addEventListener(MouseEvent.CLICK, onClick);
Add this to your code on each button before playing a sound:
SoundMixer.stopAll();
If you're adding actions directly from the timeline in Adobe Flash, there's no need to import the class. If you're working from an IDE like FlashDevelop or FlashBuilder, add this code to the beginning (after Package {):
import flash.media.SoundMixer;
Happy coding!
Edit: More info on the SoundMixer class

How to access a frame inside a movieclip from within another movieclip

I have a problem regarding movieclips...
On my main timeline there are 2 MCs and inside MC1 there is a button, which - when clicked - should get me to Frame 10 of MC2 (on the main timeline)...
My button code (inside mc1):
btn_standard.addEventListener(MouseEvent.CLICK, standard_click);
function standard_click(myNextEvent:MouseEvent):void {
MovieClip(root).mc2.gotoAndPlay(10);
}
There is no error, but however the button won't work...
Can anyone please help me! :-(
EDIT: Here's a sample file - same problem!
Why don't you add as3 code on main timeline instead of inside of mc1, like this:
mc1.btn_standard.addEventListener(MouseEvent.CLICK, standard_click);
function standard_click(myNextEvent:MouseEvent):void {
mc2.gotoAndPlay(10);
}
This code wont work because in the 1-st frame there is no mc2:
function standard_click(myNextEvent:MouseEvent):void {
MovieClip(root).mc2.gotoAndStop(10);
}
Dispatch any event from this MovieClip after mouse click:
function standard_click(myNextEvent:MouseEvent):void {
dispatchEvent(new Event(Event.COMPLETE));
}
Add next code to the first frame of the main timeline:
import flash.events.Event;
stop();
mc1.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void
{
gotoAndStop(10);
mc2.gotoAndStop(10);
}

Adobe Flash Pro CS6 movie looping after adding code to actionpanel

i have got a problem and cannot figure out. Everything is fine when i test the scene, i have added stop(); to my movie clip timeline and everything is fine until no code is added to the main stage. As soon as i start adding code to the stage, it starts looping? Because of this i'm stuck with a problem and cannot even develop further, because even mouse click events wont work of that looping... The code i am trying to add is just a simple move layer off stage after click on play button:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
introScreen.play_btn.addEventListener(MouseEvent.CLICK, clickAway);
function clickAway(event:MouseEvent):void
{
moveScreenOff(introScreen);
}
function moveScreenOff(screen:MovieClip):void
{
//Move the screen off...
var introTween = new Tween(screen,"x",Strong.easeInOut,screen.x,(screen.width)*-1,1,true);
//When the motion has finished...
introTween.addEventListener(TweenEvent.MOTION_FINISH, tweenFinish);
function tweenFinish(e:TweenEvent):void
{
trace("tweenFinish");
//Establish the game state...
gameState = STATE_INIT_GAME;
trace(gameState);
//Fire off the gameLoop function at the frame rate of the movie...
addEventListener(Event.ENTER_FRAME, gameLoop);
}
}
The reason your animations are looping continuously is because you have errors in your code.
Access of undefined property gameState.
Access of undefined property STATE_INIT_GAME.
Access of undefined property gameState.
Access of undefined property gameLoop.
You are trying to reference members that haven't been created yet or don't exist. Your main timeline starts at frame 1 with the code you referenced trying to access those variables and the gameLoop method. You need to setup the variables in the first frame to allow them to be referenced. i.e. under your import statements:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var gameState:String;
const STATE_INIT_GAME:String = "GAME_INIT";
then you need to add the gameLoop function:
function gameLoop(e:Event):void {
trace('game loop running');
}
If you don't want those variables and that function setup there then you need to go to the frame using gotoAndStop or gotoAndPlay where those functions and variables can be declared and/or initialized, and stored into memory. Then they will be accessible.

How to access variables inside a movie clip on the main timeline?

Ok so i have some variables called "stat" that is inside a movie clip that i need to access from the main timeline. I have tried multiple ways but none of them have worked.
Edited.
I put the stage instance Movieclip names "mc". and this is have a this script.
var stat:String ="Test";
and next following script, Main timeline. If you access mc.stat you not get value. console show to null. when you called In Main timeline script access to instance MovieClip inner variable. because maybe Initialization code inside the script does not work yet. so you should delay called.
I suggested Using the Timer. try this:
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
trace("check1:" + mc.stat);
var timer:Timer = new Timer(1, 1);
timer.addEventListener(TimerEvent.TIMER, onAdded);
timer.start();
function onAdded(e:TimerEvent):void
{
timer.removeEventListener(TimerEvent.TIMER, onAdded);
trace("check2:" + mc.stat);
}
Code is always helpfull!
The movieclip musst exist at the time the code is executed.
trace("stat value in mc = " + mcName.stat);

Flash CS6 Error #1009 - Button disappears

I'm starting to use Flash CS6 for the first time to try and make Scaleform UI's for UDK. I'm following this simple tutorial: http://goo.gl/yedMU. I've followed it to the letter but can't seem to get it to work. I even have tried it again in a new project but it ends up with the same error. I've triple checked each name and instance but it just refuses to work. Here is the really simple code of the two frames in the file:
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.MovieClip;
subMenu_btn.addEventListener(MouseEvent.CLICK, subMenu);
exit_btn.addEventListener(MouseEvent.CLICK, exitGame);
var cursor:cursor_mc = new cursor_mc();
addChild(cursor);
cursor.x = mouseX;
cursor.y = mouseY;
cursor.startDrag();
stop();
function subMenu(event:MouseEvent):void
{
gotoAndStop('Sub Menu');
}
function exitGame(event:MouseEvent):void
{
fscommand('ExitGame');
}
and
play_btn.addEventListener(MouseEvent.CLICK, playGame);
back_btn.addEventListener(MouseEvent.CLICK, backBtn);
function playGame(event:MouseEvent):void
{
fscommand('PlayMap');
}
function backBtn(event:MouseEvent):void
{
gotoAndStop('Main Menu');
}
I used the debugger and the code breaks at
exit_btn.addEventListener(MouseEvent.CLICK, exitGame);
Any ideas? The whole thing works until I used the 'Back' button to go back to the first frame, when the 'Exit' button is gone and I get that error. The 'Submenu' button remains however and the menu is still operable.
This is the error using the debugger:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Menu_fla::MainTimeline/frame1()[Menu_fla.MainTimeline::frame1:6]
at flash.display::MovieClip/gotoAndStop()
at Menu_fla::MainTimeline/backBtn()[Menu_fla.MainTimeline::frame2:10]
Okay, so I might have been wrong in what I said in the comments above, the solution is this:
For your first frame's AS3:
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.MovieClip;
var cursor:cursor_mc = new cursor_mc();
subMenu_btn.addEventListener(MouseEvent.CLICK, subMenu);
exit_btn.addEventListener(MouseEvent.CLICK, exitGame);
addChild(cursor);
cursor.x = mouseX;
cursor.y = mouseY;
cursor.startDrag();
Mouse.hide();
stop();
function subMenu(event:MouseEvent):void
{
subMenu_btn.removeEventListener(MouseEvent.CLICK, subMenu);
exit_btn.removeEventListener(MouseEvent.CLICK, exitGame);
removeChild(cursor);
gotoAndStop('Sub Menu');
}
function exitGame(event:MouseEvent):void
{
fscommand('ExitGame');
}
For your second frame's AS3:
play_btn.addEventListener(MouseEvent.CLICK, playGame);
back_btn.addEventListener(MouseEvent.CLICK, backBtn);
cursor = new cursor_mc();
addChild(cursor);
cursor.x = mouseX;
cursor.y = mouseY;
cursor.startDrag();
Mouse.hide();
function playGame(event:MouseEvent):void
{
fscommand('PlayMap');
}
function backBtn(event:MouseEvent):void
{
removeChild(cursor);
gotoAndStop('Main Menu');
}
You were instantiating the cursor each time you hit frame 1, which I believe would create a namespace problem. The solution was to remove the cursor from the stage, and add it back in for each frame. There may be a more elegant solution, but as I never use multiple frames with AS (for this very reason), this is the best I could do. I also hid the mouse cursor to give your cursor_mc more focus.
Let me know if you have any other questions.
Happy coding!
Another solution would be to create a new layer, and put the cursor code on that layer only. That layer would have one keyframe, and extend to cover the entire timeline, so it is always alive.
Or, create a menu movie clip that exists on frame 1. And inside that movie clip, have your different frames for each part of the menu (options, etc.). And the cursor would exist at the same level as the menu movie clip. So it always exists and is only initialized once.