Why do my animation sounds play in the beginning of test movie? - actionscript-3

I have a game where a cutscene comes in a specific order and after you press the play button, the play button page gets removed and we add another child which is the "animation movieclip page". This movieclip is a page with animations and sounds that play inside of it.
At first the menu page with the buttons come up, but animation page doesn't come up unless you press play.
Why do my sounds from the animation movieclip come up when it's not even added to the stage yet?
So the sounds from the animation movieclip played before adding to stage.

I can't help you without looking your coding. And i use imagination
to solve your problem.
Try adding addEventListener(Event.ADDED_TO_STAGE,init);
Example
public class MyClass extends AnimationClip {
public function MyClass():void {
if(stage) {
init();
} else {
addEventListener(Event.ADDED_TO_STAGE,init);
}
}
Hope this helpful.

Related

How do I add MovieClips to the stage while using AIR?

So I had been messing around in AS3 for awhile and had done some basic starting flash game things, but then found out I could use air to make them for mobile platforms. So I tried copying over what I had worked on before, but I am running into issues.
Right now I am just trying to find out how to add an instance of a MovieClip to the stage. I have an image converted to a MovieClip with it's own class so I can manipulate it later (such as moving around) and I have this in my Main class:
class Main extends MovieClip
{
var space:Background; //Background is the MovieClip
function Main():void
{
space = new Background(stage);
stage.addChild(space);
}
}
But when I run the program, the image doesn't show up. It works that way when I just had a standard AS3 program and I don't understand why it would be different when I am trying to use AIR. Any assistance would be greatly appreciated, thank you.
Put the word 'public' in front of 'class' in your first line.
Also remove the 'stage' from the first line in your constructor: make it space = new Background();
And do you really need 'stage' in the addChild statement? Unnecessarily verbose!
I can run this code through the AIR compiler (AIR SDK 3.8 or later):
package
{
import flash.display.MovieClip;
public class MAIN extends MovieClip
{
var space:Background;
public function MAIN()
{
space = new Background();
stage.addChild(space);
}
}
}
With an image inside the Background class it shows up fine. My 'Background' is a library symbol linked for Actionscript. If your 'Background' is pure Actionscript are you adding a graphic to it?
Rajneesh Gaikwad had the answer, I didn't have my Main class as my Document class.

Targeting Root from a Movieclip

Im having trouble targeting back to the main timeline from within a movie clip AS3.
I have tried the code below but still does nothing? I have labelled the frame in which I would like it to go to on the main time and added this code to the actions within in the movie clip, but when I click the button it does nothing? I have no errors come up?
Any ideas?
map_UK.addEventListener(MouseEvent.CLICK, MAIN);
function MAIN(e:MouseEvent):void
{
MovieClip(root).gotoAndPlay("MAIN");
}

Why isn't this click event being fired?

Why isn't the myMouseClick event being fired?
myMC:TestMC = new TestMC();
myMC.addEventListener(MouseEvent.CLICK, myMouseClick);
addChild(myMC);
function myMouseClick(e:MouseEvent):void {
trace("clicked");
}
As far as I can tell from the tutorials I've seen, that should work. For a moment, I thought that since I was adding the event listener to myMC, I needed to have the event function inside the myMC class, but that didn't work. Just gave an error about accessing an undefined property.
If it helps any, TestMC is a seperate .as file that extends movie clip.
I'm just trying to make it so when the movie clip itself is clicked, it does something. The movie clip itself will be following the mouse.
The object I was trying to click was made up on vertical lines. Apparently the whole movie clip isn't a collider... just the pixels in it, so when I was clicking, I wouldn't hitting enough of it. Changing it to a box worked. I could probably nest it if I wanted the same design, but that's fine.

AS3 Issues: Using a button to call a specific label frame on another movieclip

I am trying to figure out how to make it so I can click a button on the main timeline and have it jump to a frame inside a separate movieclip on the main timeline. This is the goofy code I have at the moment, but this is after a lot of changes, so who knows where I am right now. This is for a simple virtual pet game and I'm not sure why I'm having such a hard time with this particular issue. I'm missing something big.
function Shower(event:MouseEvent):void {
MovieClip(this.Egg).gotoAndPlay("shower");
}
// buttons
clean_btn.addEventListener(MouseEvent.CLICK, Shower);
It seems like you're writing this on the Timeline, and not in a class. You don't need MovieClip(this.Egg) to access the movieclip you're trying to play. Instead, it should have an Instance Name (for example, "my_mc"), and you can just call it like this:
function Shower(event:MouseEvent):void {
my_mc.gotoAndPlay("shower");
}
// buttons
clean_btn.addEventListener(MouseEvent.CLICK, Shower);

Simulating a MovieClip in pure as3

I am looking for a way to make a Flash movie clip (animation, like the ones created with Flash Pro CS), but purely in as3 - so I can import them into Prezi.
I have done a lot of as3 programming in Flash Builder with Flex projects and I have no background in how MovieClips work.
What I have already tried is extending a MovieClip class and trying to base the animation on Timers, this failed so I tried with ENTER_FRAME event (because flash animations are based on frames - so I thought...). But all this fails, only graphics drawn in the constructor are displayed - no animation happens. (As I wrote in the first paragraph I am testing this importing the swf into Prezi, opening it in a browser works as expected)
Is there any way to do it? Like listening to specific events?
Give sprite sheet a try. It's the best solution for animation in AS3, and also pretty simple to implement. for changing drawing, there are Timer and ENTER_FRAME event to do this.
Funny thing happened. I wanted to show you a sample code I was trying out (I already tried Sprite with ENTER_FRAME), that was not working. By accident I found a solution. It looks like you need to draw something in the first frame, or else the other frames won't work (in Prezi at least).
So here is the working code:
public class PreziTest extends Sprite{
private var radius:uint = 10;
public function PreziTest(){
addEventListener(Event.ENTER_FRAME, onEnterFrame);
onEnterFrame(null); // WITHOUT THIS IT WON'T WORK - YOU NEED TO DRAW SOMTHING IN THE FIRST FRAME
}
private function onEnterFrame(event:Event):void{
radius += 10;
if(radius > 200)
radius = 10;
graphics.clear();
graphics.beginFill(0xff0000);
graphics.drawCircle(radius, radius, radius);
}
}
Thanks for all your help!