Dynamic Text Boxes disappearing in AS3 - actionscript-3

I've got a small interactive flash animation, there's a button that allows the user to gain points then clicked, then another to go to a "Shop" when clicked. the only problem is that the Dynamic text box doesn't show the value on the second frame. All the actionscript is on one Layer, throughout the entire file.
var money = 100;
var shopbtn;
cash_txt.text = money;
stop();
Button.addEventListener(MouseEvent.CLICK, MoneyGet);
function MoneyGet (evt: MouseEvent){
money += 50;
cash_txt.text = money;
}
shopbtn.addEventListener(MouseEvent.CLICK, Shop);
function Shop (evt: MouseEvent){
cash_txt.text = money;
gotoAndStop(2);
}
I've checked if embedded fonts is working, and it's fine. Thanks.
I'm using Professional CC.

Related

AS3 - Interactive Bingo Grid

I'm working on a Flash project that will be used to display numbers that are called for a game of Bingo. A person will control the board being displayed to a live audience by clicking on one of 75 "covered" tiles. Upon click of a tile (ex: button1), 3 things need to happen:
1) the click triggers a short MovieClip (ex: Number1) animation that takes up most of the screen showing "1"
2) the MovieClip (Number1) unloads itself and we see the grid again
3) The tile covering the "1" on the grid is removed so that we see which numbers have been called
I have the following code that is working correctly to remove the individual [!tiles upon click:
button1.addEventListener (MouseEvent.CLICK, Reveal1);
function Reveal1(event:MouseEvent) {
if (button1.alpha == 1){
button1.alpha = 0;} else {button1.alpha = 1}
}
What is the simplest way for me to load/unload a MovieClip for each of the 75 numbers when their respective tiles are clicked?
Event.currentTarget is the reference to the button that is the event source.
button1.addEventListener(MouseEvent.CLICK, Reveal1);
function Reveal1(event:MouseEvent)
{
var aButton:InteractiveObject = event.currentTarget as InteractiveObject;
aButton.alpha == (aButton.alpha == 1)? 0: 1;
// and/or
aButton.parent.removeChild(aButton);
}

How to make a score in a dynamic textfield display on another frame?

I'am trying to make a scoring system by using a timer so that it work on the basis that as the player keeps on going the timer/score keeps on going up and I got that to work with this code.
var nCount:Number = 0;
var myScore:Timer = new Timer(10, nCount);
score_txt.text = nCount.toString();
myScore.start();
myScore.addEventListener(TimerEvent.TIMER, countdown);
function countdown(e:TimerEvent):void{
nCount++;
score_txt.text = nCount.toString();
}
However say for example the player crashes I want the game to remember the score and then display it on another frame where I have a game over screen so that it shows the final score to the player and this is the part that I have no idea how to do. Any help would be appreciated.
Many Thanks
You can't modify objects on "another frame". You can only modify objects on the current frame. The frames simply represent state that is baked into the SWF to be actualized when the timeline reaches those frames; you cannot change SWF frame data at runtime. You can only change the resulting objects once those frames are constructed by the player.
What you can do is store your score in a variable (available across all frames) and simply set the correct text objects when you are on those frames.
For your code, you simply need to only set the nCount to 0 when the SWF loads (or you want to reset it), and set the score_txt immediately on the frame it exists. For example, you could do it like this:
// Don't initialize a value, that would overwrite
// it every time this frame is visited
var nCount:Number;
// Only the first time, when the value is NaN, set the score to 0
if(isNaN(nCount)){
nCount = 0;
}
// Immediately show the current score text
score_txt.text = nCount.toString();
// The rest stays the same
var myScore:Timer = new Timer(10, nCount);
score_txt.text = nCount.toString();
myScore.start();
myScore.addEventListener(TimerEvent.TIMER, countdown);
function countdown(e:TimerEvent):void{
nCount++;
score_txt.text = nCount.toString();
}

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.

2 distinct event triggered by one button - Flash and AS3

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

Making multiple objects draggable

I have about 50 symbols that I want to make draggable. Nothing fancy, just the ability to click it and drag it to a different location.
I found as3 code for doing so but when I paste it into my file it gives me errors:
**Error** Scene=Scene 1, layer=Units, frame=1:Line 9: The class or interface 'MouseEvent' could not be loaded.
function mouseDownHandler(evt:MouseEvent):void {
That code is:
// Register mouse event functions
fighter_uk.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
// Define a mouse down handler (user is dragging)
function mouseDownHandler(evt:MouseEvent):void {
var object = evt.target;
// we should limit dragging to the area inside the canvas
object.startDrag();
}
function mouseUpHandler(evt:MouseEvent):void {
var obj = evt.target;
obj.stopDrag();
}
I'm using flash pro 8, so I tried finding as2 code but couldn't find it.
Also, is there an 'easy' way to code all 50 objects?
I think you're trying to compile AS3 code with AS2 compiler. Try changing your compilation settings to target AS3.
Also you may need to include the class import at the top of your code:
import flash.events.MouseEvent;
To drag 50 objects, add them all on the same container sprite and add the listener to the container sprite only:
var holder:Sprite = new Sprite();
for ( var i:int = 0, l:int = 50; i < l; i++ ) {
var dragee:YOUR_CUSTOM_OBJECT = new YOUR_CUSTOM_OBJECT();
holder.addChild(dragee);
}
addChild(holder);
holder.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
holder.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
holder.addEventListener(Event.MOUSE_LEAVE, mouseUpHandler);
var currentDragee:YOUR_CUSTOM_OBJECT = null;
function mouseDownHandler(evt:MouseEvent):void {
currentDragee = evt.target as YOUR_CUSTOM_OBJECT;
if ( currentDragee !== null ) {
currentDragee.startDrag();
holder.addChild(currentDragee); // bring current to front position
}
}
function mouseUpHandler(evt:Event):void {
if ( currentDragee !== null ) currentDragee.stopDrag();
currentDragee = null;
}
YOUR_CUSTOM_OBJECT being the object class you need to drag. Hope it helps!
This page seems to have the answers you are looking for (AS2 drag and drop). If you've already seen it, you'll need to explain why it's not good enough for your needs.
If you want to drag/drop multiple instances in AS2, you can still add the code to the movieClip symbol, export it from the library and load the instances up using attachMovie (all 50 of them). If they are all different, then attach the code as necessary to the clips themselves, or to some function elsewhere that will capture all the clicks and decide what was clicked. This is all very doable in AS2.
Remember you can use your onClipEvent(load) function to set up a lot of the initial lifting.
Here's a sample I made in AS2 for making a node tree. It's all draggable (mouse drag) and zoomable (with mouse Wheel). You can add nodes by clicking on the little down arrow in the node box. Each node is listening for the mouse.
You'll want to look at this section for the most part:
// Enable drag on button press
on (press)
{
startDrag(this);
}
// Stop the drag on release of mouse button
on (release)
{
stopDrag();
}
Besides this, I'm not really sure how your setup looks, so I hope this helps get the ball rolling. (Check the link, there's lots of little gems in there).
Flash Professional 8 only supports ActionScript 2 & 1
You can follow this official URL and learn how to do that in ActionScript 2, but I extremely recommend you to work with ActionScript 3.