2 distinct event triggered by one button - Flash and AS3 - actionscript-3

I am completely new using Flash and I am doing a project to enhance my skills.
I wonder if there's a way to connect a button doing 2 different things.
I created a slide button that goes on X axis only using Gesture (Touch) and also I created a movie clip showing a map progressing (changes) I would like to connect them, something like every single frame this button goes on this slide it also plays one frame inside the movie clip and return of course.
I think I made myself clear, as it goes along the axis it plays the movie clip.
is there a way to approach for this idea?
ps:
It looks very advanced

MySprite.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
MySprite.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
MySprite.addEventListener(Event.ENTER_FRAME, fixer);
function onTouchBegin(e:TouchEvent) {
e.target.startTouchDrag(e.touchPointID);
trace("touch begin");
}
function onTouchEnd(e:TouchEvent) {
e.target.stopTouchDrag(e.touchPointID);
trace("touch end");
}
function fixer(e:Event) {
e.currentTarget.x = [int here];
if(runIt){
e.currentyTarget.x += speed;
}
}

Related

setchildindex is creating problems

I have made a simple drag and match game for kids.
I used setchildindex for movie clips to be dragged but when I click next button and go to another frame but movie clips are remaining in the same stage. What should i do?
Here is my code I used: (drag_1, this.numChildren0);.
When I reload it's not working.
drag_1.buttonMode = true;
drag_1.addEventListener(MouseEvent.MOUSE_UP, dropMe_1);
drag_1.addEventListener(MouseEvent.MOUSE_DOWN, dragMe_1);
var back_1X:Number = back_1.x;
var back_1Y:Number = back_1.y;
var hit_2X:Number = hit_2.x;
var hit_2Y:Number = hit_2.y;
function dragMe_1(event:MouseEvent)
{
drag_1.startDrag()
setChildIndex(drag_1, this.numChildren-1);
}
function dropMe_1(event:MouseEvent)
{
drag_1.stopDrag();
if(drag_1.hitTestObject(drop_2))
{
TweenMax.to(drag_1, 0.5, {
x:hit_2X,
y:hit_2Y,
ease:Cubic.easeOut
});
drag_1.mouseEnabled = false;
SoundMixer.stopAll();
}
else
{
TweenMax.to(drag_1, 0.5,
{
x:back_1X,
y:back_1Y,
ease:Bounce.easeOut
});
}
}
You need to remove the MovieClips using removeChild().
Now, why do you need to do that here? Well, this is one of those odd problems you get when you mix the timeline with code. When you place a symbol on the timeline keyframe, the Flash Player will instantiate that symbol when it reaches that frame. After that, any frame on the timeline that updates the symbol (tweens, effects, etc) will do just that, and any frame that lacks the symbol will remove it. However, the Flash Player is very picky about identifying that symbol on each frame of the timeline. When you move it using setChildIndex you are basically breaking the timeline link, and the Flash Player no longer identifies it and removes it based on the keyframes. You'll also find that if you revisit a keyframe that had that symbol, the Flash Player will instantiate a second one regardless if the one you moved is still there. As you can see, it can get pretty messy.

button inside a movieclip that changes depending on the frame

I'm making a dynamic map of France going from Roman times to 2014 and I have a problem.
Most of my interface is working except for one feature that requires a lot of mc inside the main movieclip.
I can't post pictures so i'll try to explain what i did.
I have a main movieclip ("france_map"). Inside it, i have several layers including one called "rulers". In this layer, I have the timeline of every king/president of France.
We'll focus on two of them: Sarkozy (frame 2007 to frame 2012) and Hollande (frame 2013 to frame 2014).
The mc "sarkozy" is only accessible between 2007 and 2012. On frame 2013, it disappears, replaced by the mc "hollande".
When I click on one of them from root, a biography of the one concerned opens.
It works thanks to that code:
france_map.sarkozy_btn.addEventListener(MouseEvent.CLICK, fl_sarkozy_btn);
function fl_sarkozy_btn(MouseEvent: Event): void {
sarkozy.visible = true;
close_fiches_btn.visible = true;
close_arbre_btn.visible = false;
sarkozy_txt.visible = true;
};
france_map.hollande_btn.addEventListener(MouseEvent.CLICK, fl_hollande_btn);
function fl_hollande_btn(MouseEvent: Event): void {
hollande.visible = true;
close_fiches_btn.visible = true;
close_arbre_btn.visible = false;
hollande_txt.visible = true;
};
But, I have two button I use to navigate the timeline (controlled by that code:
/* Avancer ou Reculer d'un an */
flecheg_sym.addEventListener(MouseEvent.CLICK, fl_gotoprev);
function fl_gotoprev(MouseEvent: Event): void {
for each(var item: MovieClip in maps) {
if (item.currentFrame > 0) {
item.prevFrame();
} else {
item.nextFrame();
}
}
fleched_sym.addEventListener(MouseEvent.CLICK, fl_gotonext);
function fl_gotonext(MouseEvent: Event): void {
for each(var item: MovieClip in maps) {
if (item.currentFrame > 0) {
item.nextFrame();
} else {
item.prevFrame();
}
}
}
When I use these button, all timelines move together. It works.
My problem is that:
when I reach frame 2013 ("sarkozy" disappears, "hollande" appears), the new button ("hollande") doesn't work. And when I go back to 2012 ("hollande" disappears, "sarkozy" appears), "sarkozy" doesn't work anymore either.
I don't understand the problem (i'm fairly new at as3 and flash).
If i wasn't clea enough (english is not my first language), tell me, i'll try to explain more.
Thanks for your help.
Jeryl
And when I go back to 2012, "sarkozy" doesn't work anymore either
If you want to use MovieClip as a navigation instrument you should master it. When you go back from current keyframe that contains new assets and logic, and I assume previous frame isn't keyframe with listeners and logic, you will see graphics without any binded scripts. Briefly speaking, reassign your listeners for the buttons, use constant movieclip names, design your timeline appropriately and all be ok.

"Smooth and Regular" scroll in flash

I am working on a news ticker in Flash AS3. The code is reading some RSS from CNN and generates a MovieClip with headline text one after another. Now this movieclip starts scrolling from right to left. For scrolling I am using TweenLite. I don't want any easing, just want a regular and smooth scrolling.
I have used both options:
TweenLite.to(news_mc, 60, {x:minX} );
and
TweenLite.to(news_mc, 60, {x:minX, ease:Linear.easeNone} );
But, in both case, the animation is not smooth. It starts scrolling speedily and at last it becomes very slow.
I've seen something similar to this. Are you implementing your TweenLite call in a loop? You should only be calling for a tween on the target object once. Only once that tween is finished or you want a brand new tween on that object should you call TweenLite.to with that target object again.
Seeing as how you're only after a linear tween, would it not be simpler to simply increment/decrement the target's X every frame? Something like this:
this.addEventListener(Event.ENTER_FRAME, onEnterFrame)
private function onEnterFrame(e:Event):void
{
if(news_mc.x > minX)
{
news_mc.x -= 3 //This is a shot in the dark, tweak this for your values
}
else
{
news_mc.x = maxX
}
}

AS3 Button to stop Movieclip after its finished playing

Ok, so I'm a beginner at AS3 and Flash and I managed to put this code together for an animation. A Button called start_btn is supposed to start and stop a movieclip called main_mc. On the first click of the Button, the Movieclip is supposed to play (which it does), however on the second click, the movie stops in the middle of its animation (which I don't want). My question is, when you click the Button a second time, how can i get the Movieclip to finish playing its animation then stop on the last frame?
I thought about using if (main_mc.currentFrame == main_mc.totalFrames); {main_mc.stop(); but the Movieclip still does not stop on the last frame. The Movieclip itself also has a gotoAndPlay(2); command on the last frame so that the animation repeats before the Button is clicked a second time.
here is the code i have:
`start_btn.addEventListener(MouseEvent.CLICK, mainaniS);
function mainaniS(event:MouseEvent):void
{
main_mc.play();
start_btn.removeEventListener(MouseEvent.CLICK, mainaniS);
start_btn.addEventListener(MouseEvent.CLICK, mainaniSt);
}
function mainaniSt(event:MouseEvent):void
{
if (main_mc.currentFrame == main_mc.totalFrames);
{main_mc.stop();}
start_btn.removeEventListener(MouseEvent.CLICK, mainaniSt);
start_btn.addEventListener(MouseEvent.CLICK, mainaniS);
}`
Try main_mc.gotoAndStop(main_mc.totalFrames).
I was going to provide a quick and dirty solution, but decided instead to try and explain a few of the issues with your current implementation and attempt to refactor and explain and better one. Unfortunately I don't have access to Flash right now, so the code is untested.
You're adding and removing event listeners often, which is generally a bad idea. Instead, since you're using a single button to perform multiple functions it would make sense to track the button state in a separate variable. In this case, a boolean for whether or not the movieclip is currently playing.
var playing:Boolean;
Now we can combine the mainaniS and mainaniSt into one and perform a different action based on whether or not the movieclip is playing, and just keep the one eventlistener on the button. I've also taken the liberty of naming the method something more meaningful:
start_btn.addEventListener(MouseEvent.CLICK, onStartClick);
function onStartClick(event:MouseEvent):void
{
if(playing) {
playing = false;
}
else {
playing = true;
main_mc.play();
}
}
You may be wondering why we don't call main_mc.stop() in the first block: the reason is that you don't want to stop the movieclip as soon as you click the button, but after the movieclip has finished playing if the button has been clicked. Therefore, we just set playing to false to indicate that we want it to stop later.
Finally, we need to make sure the movieclip stops upon completion, but only if playing is false. To do this we add a listener to movieclip that is called every frame, and checks whether playing is false, and if it's on the last frame. Note that the last frame is actually totalFrames - 1: this is because the frame numbers start from zero rather than one (i.e. if totalFrames is 3, the frame numbers will be 0, 1, 2).
main_mc.addEventListener(Event.ENTER_FRAME, animate);
function animate(event:Event):void {
if(!playing && main_mc.currentFrame == main_mc.totalFrames - 1) {
main_mc.stop();
}
}
All the refactored code together:
var playing:Boolean;
start_btn.addEventListener(MouseEvent.CLICK, onStartClick);
main_mc.addEventListener(Event.ENTER_FRAME, animate);
function onStartClick(event:MouseEvent):void
{
if(playing) {
playing = false;
}
else {
playing = true;
main_mc.play();
}
}
function animate(event:Event):void {
if(!playing && main_mc.currentFrame == main_mc.totalFrames - 1) {
main_mc.stop();
}
}

Timeline instances not on first frame

It's been a while since I've had to write Actionscript that really needs to integrate with the timeline (in this case, controlling a series of frames that must happen in a certain sequence) and I am trying to figure out what to do.
In the first few frames, I have a button "next_1".
At frame 10, I need to have another button "next_2". I really really need this button to not be on frame one (I could possibly just make it invisible, but that's going to create a clickable area that I don't want).
The problem is, anything I don't put on "frame_1" renders as null in my Document class.
Is there any solution to this? I would rather not have to write my script on the timeline if possible (it seems easier in the long run to keep it in a document class)...
Items on the timeline are created on the fly, so if the playhead has not reached frame 10, next_2 is not created.
Easiest Document-class solution:
Create an array of frame labels like ["label1", "label2"]
Create sectionIndex var and set it to 0
Create a next button on its own layer so it is always showing.
When the next button is clicked, increment sectionIndex, then gotoAndPlay(myLabels[sectionIndex])
Okay, directly lifted from "Real World Flash Game Development":
/**************************************************
* FRAME LABELS *
**************************************************/
private function enumerateFrameLabels():void {
for each (var label:FrameLabel in currentLabels) {
addFrameScript(label.frame-1, dispatchFrameEvent);
}
}
private function dispatchFrameEvent():void {
dispatchEvent(new Event(currentLabel, true));
}
This dispatches an event at each frame label on the timeline.
Then you can just add event listeners for each frame:
addEventListener("name_of_my_framelabel", frameHandler);
addEventListener("another_framelabel", frameHandler);
And write a switch statement to add event listeners for the buttons when they actually show up on the timeline.
private function frameHandler(e:Event):void {
switch(e.type) {
case 'screen_2':
stop();
next_2.addEventListener(MouseEvent.CLICK, click2, false, 0, true)
break;
}
}