Trigger a function when specific frame number is reached - AS3 - actionscript-3

I wanted friends, do the following when my MC get a determanido internal quandro, it triggers a function to another MC. for example when the ball hit the wall, a person goes to search - la, I have tried using :
("root") {root.MC.play ()}
Translation from comments:
Friends, I have an MC_1 with 10 frames, when it reaches frame 5, I want another movieClip MC_2 to respond (eg: to move or fade etc)

creating a ENTER_FRAME listener for your MC_1 is the easiest way to achieve this
MC_1.addEventListener(Event.ENTER_FRAME,respond);
function respond(e:Event):void{
if(MC_1.currentFrame>=5)
MC_2.gotoAndPlay(2);
//or any other respose you want from MC_2
}

addFrameScript can be used in AS3 to specify a function to execute on reaching a MovieClip frame.
MovieClip.addFrameScript(index:int, func:Function);
A sample implementation of this:
// addFrameScript's index is zero Based, hence 4 means frame 5
MC_1.addFrameScript(4, funcToExecute);
function funcToExecute():void{
// this will get called when MC_1 reaches frame 5
// do stuff here, like manipulating MC_2, etc...
}

Related

Jumping back and forth inside a Movieclip with a button?

So, I'm very much a beginner in AS3. I've been reading and figuring out things as I go, though I can't wrap my head around this.
So, I have 4 frames. Each frame has a different movie clip, MC1,MC2,MC3,MC4
Inside those four movie clips, there is another movie clip with the same instance name for each: BC, and inside that movie clip there are two frames. Frame 1 has a dot, and frame 2 does not.
MC1>BC>(2 frames)
MC2>BC>(2 frames)
and so on....
What I'm trying to do: I wanted to see if there was any way to control the frame navigation of BC inside all four MC clips at the same time with one button.
I want to switch back and fourth between the two frames inside the BC movie clip.
I'm at a loss, I've tried quite a few things.
You should be able to do so by giving them all the same instance name (so long as there is only ever one of them on screen at once).
So lets say you have a button that spans all 4 frames with an instance name of navBtn and you gave each of the MC1-4 clips the same instance name of MC. You could do the following on frame 1:
navBtn.addEventListener(MouseEvent.CLICK, navBtnClick);
function navBtnClick(e:Event):void {
if(MC.BC.currentFrame == 2){
MC.BC.gotoAndStop(1);
}else{
MC.BC.gotoAndStop(2);
}
}
Reading your question again, perhaps what are looking for is to have each clip automatically go to the same frame for their BC child when they load? If that is the case, then follow the example in the comment on your question by #Organis. Here is one way you could accomplish this:
Create two variable on frame one of your main timeline:
var BC_NAV_CHANGE:String = "BC_NAV_CHANGE";
var BC_CurFrame:int = 1;
Then, when you need to change the frame of the BC objects, do the following:
//create a function that you call when you want to change the BC frame
function toggleBCFrame(e:Event = null){
MovieClip(root).BC_CurFrame = MovieClip(root).BC_CurFrame == 1 ? 2 : 1;
//the line above is a if/else shorthand, that is setting a new value to the `BC_CurFrame` var,
//if the current value is `1`, it will set it to `2`, otherwise it will set it to `1`
MovieClip(root).dispatchEvent(new Event(MovieClip(root).BC_NAV_CHANGE));
//this line (above) dispatches a event telling anything that's listening that the variable has changed
}
If the code above is on the main timeline, you can forgo all the MovieClip(root). parts of the code.
Now, on the timeline of your BC MovieClip(s), put the following code:
//create a function that goes to and stops at the frame stored in the global variable
function updateFrame(e:Event = null){
gotoAndStop(MovieClip(root).BC_CurFrame);
}
//next listen for the BC_NAV_CHANGE event, and call the above update function above any time that event happens
MovieClip(root).addEventListener(MovieClip(root).BC_NAV_CHANGE, updateFrame);
//lastly, call the update function right away so when the BC clips loads it immediately goes to the correct frame
updateFrame();

Button Click Counter not incrementing

I have a similar animation in Flash with a replay button at the bottom. Once the animation completes, it does not loop. Instead if you click replay, the animation starts again.
I want a textbox which will display the amount of times the button was pressed.
My existing code is currently replaying the animation. It's incrementing the count to 1 (from 0) in trace but not getting set within the textbox. Additionally, if I click the button again, trace displays 1 again -- no increment occurred (1 to 2, 2 to 3, etc).
If I comment out gotoAndPlay(1), the incrementing works just fine and is displayed in the textbox -- but the animation does not play again.
What am I doing wrong?
import flash.events.MouseEvent;
var hitcount:Number = 0;
textCounter.text = String(hitcount);
function incCounter(event:MouseEvent):void
{
hitcount++;
textCounter.text = String(hitcount);
trace(hitcount);
gotoAndPlay(1);
}
replay_btn.addEventListener(MouseEvent.CLICK, incCounter);
The problem in your code is that you are creating and initializing every time your counter, so sure you will get every replay the value 1.
So to avoid that, you can just verify if you have already created the hitcount var otherwise create it :
if(!hitcount){ // hitcount is null, so create it
var hitcount:Number = 0;
}
As a good practice, you can also remove the MouseEvent.CLICK on your button if you don't need it. So you get something like this :
if(!hitcount){
var hitcount:Number = 0;
}
count.text = hitcount.toString();
function incCounter(event:MouseEvent):void
{
event.target.removeEventListener(MouseEvent.CLICK, incCounter);
hitcount++;
count.text = hitcount.toString();
gotoAndPlay(1);
}
replay_btn.addEventListener(MouseEvent.CLICK, incCounter);
Hope that can help.
If your flash movie is longer than one frame then the layer which you are placing your actions on the stage needs to extend that amount of frames as well. Otherwise the code is no longer there after frame 1. Also when you come back to frame 1 you are attaching yet another mouse event each time. Start your code with yourButton.removeEventListener to ensure you are only ever attaching one event to it. And every time the movie ends and goes back to frame 1 your counter is reset to 1.

Timer flash to show something at the right time

I want to make a timer that will show text at my panelText (dynamic text box) at specific time, actually I have a video that I want to have subtitles, and I want to use timer, my video is 3 minutes and 37 second long, and I have script that I want to show at some time, example at 1 minute 0 seconds it will show the text "hello, this is my video to learn about solar system" in my panelText, and at 2 minute I want to show text "There are 8 planets in our solar system", something like that. For information, I'm using flvPlayback to play the video and load the external video.
An example from my code:
var myTimer:Timer = new Timer(217000);
var time = 0;
myTimer.start()
myTimer.addEventListener(TimerEvent.TIMER,timerHandle);
function timerHandle(event:TimerEvent){
if(myTimer == 120000)
{
panelText.text="There's 8 planets in our solar system";
}
and i got error 1176: Comparison between a value with static type flash.utils:Timer and a possibly unrelated type int. can someone help me?, i'm sorry for my bad english
You should use FLVPlayback's addASCuePoint() function to make precise actions when your FLVPlayback fideo is playing. You use this function to set up whatever points you want to display custom subtitles, there can be as many of these as you want, then you listen for MetadataEvent.CUE_POINT event on your FLVPlayback instance, and process the cue point in order to display the associated subtitle. Note though, you will have to separately parse subtitle expiration, or assign a cue point to remove the displayed subtitle several seconds past the displaying cue point. Although it will be better if you will be able to add subtitles directly on the video, so that seeking through your video will not require handling of misaligned subtitles. Still, it's doable with pure AS3 code, if you would also listen to VideoEvent.FAST_FORWARD and VideoEvent.REWIND events to handle user interaction with playback and display corresponding subtitle by calling findNearestCuePoint() to find the closest earlier subtitle-enabled cue point.
An example of adding a cue point:
flv.addASCuePoint(0.1,"1",{text:"Subtitle one"});
flv.addASCuePoint(6.8,"2",{text:"Subtitle two"});
flv.addASCuePoint(11.8,"2 hide",{text:""}); // remove old subtitle
flv.addASCuePoint(120.0,"3",{text:"There are 8 planets in our solar system"});
Note that each call returns an Object, which is also returned whenever a MetadataEvent.CUE_POINT event is dispatched. This has a parameters sub-object that you have passed into addASCuePoint as third parameter, which you then parse and take actions depending on what's in there. Here, I've placed a single field "text" into each of the parameter objects, which should be the text to display as subtitles. So, then you listen to the cue point event and take actions, like this:
flv.addEventListener(MetadataEvent.CUE_POINT,cueHandler);
// do this only once, a listener does not need to be added per cue point
function cueHandler(e:MetadataEvent):void {
var cuepoint:Object=e.info;
if (cuepoint.parameters) {
panelText.text=cuepoint.parameters.text;
} else {
// DEBUG here
}
}
In case of seeking, you listen for FF and rewind events with a function that will call findNearestCuePoint(), retrieve its parameters.text and place that text into your text field.
You misunderstood the usage of the Timer class.
var myTimer:Timer = new Timer(n) means that the Timer will always "tick" at the specified interval n, in your case every 217000 milliseconds (217 seconds).
This might not be the best solution, but it'll show you how you can use the timer class for a scenario such as yours.
var myTimer:Timer = new Timer(1000); //a timer that will tick every second
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
myTimer.start(); //you should set up your listener before you start the timer
function timerHandler(event:TimerEvent):void{
//myTimer.currentCount will tell you how many times your Timer has ticked
//every tick, in this scenario, represents exactly one second, because we initialized the Timer with 1000 milliseconds
//so, if you want to show some text 86 seconds after the video has started, you could do:
if(myTimer.currentCount == 86){
panelText.text = "We're 86 seconds into the video";
}else if(myTimer.currentCount == 217){
//after reading your code it seems like you intended to let the timer only run 217 seconds, because that's the length of your video
//my proposed solution could also handle this, in this else if clause
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
myTimer = null;
}
}
The error is right.you are comparing a timer with a number!!.
fixed part of code:
if(myTimer.currentCount==120000){
panelText.text="There's 8 planets in our solar system";
}
and your timer constructor is wrong!the first argument is delay and next is repeat count.
the fixed code:
var myTimer:Timer = new Timer(100,2170);
var time = 0;
myTimer.start()
myTimer.addEventListener(TimerEvent.TIMER,timerHandle);
function timerHandle(event:TimerEvent){
if(myTimer.currentCount == 1200)
{
panelText.text="There's 8 planets in our solar system";
}
in this code, our timer ticked at every 0.1 second(100 miliseconds) and is ticked 2170 times. so, to set your text, you must put a zero after your desired second.
hope this helps :)

AS3. How to change character's template in adobe flash cs5 using AS3?

I'm creating flash game. Here will be abillity to choose one of two (or more) character's. So I have in library created symbol hero. It have 7 animations on click (moving, jumping, attacking etc..)
So I want to create something like hero 2, that player could choose which one likes more. Just how to do that? Create new layer in hero and add animations or how?
I'm asking that because in Action Script 3 I'm adding hero in this case and It always will add the same:
private function create_hero()
{
addChild(Hero);
Hero.gotoAndStop("stay");
Hero.x = stage.stageWidth/2;;
Hero.y = ground.y - 60;
Hero.x_speed = 0;
Hero.y_speed = 0;
}
Maybe here is abillity to make something like that layer2.addChild(Hero);?
Or I need to create new symbol hero2? I don't like this idea, because I have long code to control hero, so for every character I'll need to dublicate code. Could you help me? Thank you.
The proper way is to dynamically create an instance Hero at game start based on the game player's selection. You indeed create two (or more) symbols in Flash CS, with common frame labeling (you can use different animation lengths for different heroes), then, once you've got your hero selection (hero1,hero2,hero3 etc, regardless of their amount) you get the class name from selection and get your Hero variable to be assigned an instance of the respective class. An example:
static var heroes:Array=[hero1,hero2,hero3]; // note: symbol names!
var Hero:MovieClip; // note, not "hero1" or anything, but general MovieClip type
public function selectHero(what:int):void {
// this is called with correct "what", design yourself. I use array index
var whatHero:Class = heroes[what]; // get selected hero symbol
if (Hero && Hero.parent) Hero.parent.removeChild(Hero);
// clean up previous hero. Drop listeners here, if any
Hero = new whatHero(); // get new hero
// process as usual, don't forget to "addChild(Hero)" somewhere
}
How this works: First, you give the player a hero selection dialogue, and call selectHero with proper value of what (0 for hero1, 1 for hero2, etc, as you make your heroes array). Then, the whatHero variable is assigned the corresponding class (yes, one can assign classes to variables in AS3!) And then the class gets instantiated via new construction, and the resultant movie clip is then assigned to Hero variable. After this is done, you can use your hero as before.

AS3 Using Many Event Listeners Causing Problems, How to Reduce?

Confusing title, my bad.
Basically, I have a list of names. Looping through, I add a MovieClip, Set 2 properties to it, the name, and an ID. The MovieClip is at the same time made to function as a button and I add 4 listeners, mouse up, over, down, or out. I do this with every name. The function each one is set to is the same.
EX: enemyButton[i].addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
The enemyID turns up "not valid property," time to time when I click, it doesn't crash at all, but sometimes I have to hit the button a few times.
I have narrowed the problem down to having to be caused by the listeners.
The function as simple as:
EX: function mouseUpHandler(e:MouseEvent):void { enemySelected(e.target.enemyID); }
My question is, is too many listeners likely to be the problem? and how can I reduce them?
Here's a snippet of the loop:
var C:Class = Class(getDefinitionByName(enemies[i]));
var c:* = new C();
c.gotoAndStop(1);
enemyButton[i].enemyID = i;
c.name = "select" + i;
c.enemyID = i;
trace(c.enemyID);
enemyButton[i].addChild(c);
enemyScroll.addChild(enemyButton[i]);
enemyButton[i].enemyName.text = info[i][Const.NAME];
enemyButton[i].setChildIndex(enemyButton[i].getChildByName("enemyName"), enemyButton[i].numChildren-1);
Thanks.
If enemyButton is a MovieClip (created via attachMovie, maybe) and not strongly typed as a EnemyButton class, then the ID property becomes dynamic. In this situation, if your list of names contains incorrect data (missing ID field, maybe), then the ID property will remain undefined on some instances of the MovieClip.
You can check the list of data used to generate movie clips. You can run into the same error if you have blank lines in your data.
This has nothing to do with event listeners.
So you just want to generate a bunch of buttons with unique properties and know what button was clicked last. Generally it is very bad idea to implement button logic outside button object. Why? Because you work with object oriented language. Good news is that you work with as3 and it treats functions as objects, so you can assign function to var like this:
var callback:Function = function(name:String, enemyId:int){ /*do something*/ }
And.. you can pass function as a parameter to another function
function setCalback(func:Function){}
button.setCallback(callback);
So, what you really need is to create your own button class, add listeners inside it, add handlers(static handlers will reduce memory usage) and pass callback function to it, that will be called when user clicks button.
Don't mean to spam this much but this was easily fixed, though the responses might have been a better method.
I just had to change target to the currentTarget, that then allowed clicking anywhere on the "button" to work. Whereas before the target varied from childs added to it.
So, solved.
Thanks for the help.