Visit frame once? - actionscript-3

I was wondering if there is a code that can make flash recognize if a certain frame has already been visited. Is there a way to go about doing this? I was thinking about making a function that traces a frame when the player is already one it. I want the function to deny entrance to that frame after it was viewed once.

Actionscript 3 doesn't care about frames. It's only flash that does.
So I would suggest keeping a global array of visited frames & populating it with frame numbers.

In frame code (I assumes you're coding in frames):
var visited:Boolean;
if (visited) return;
visited = true;
//... do frame actions

How about disabling or removing the option to go to back to a completed frame? i.e if there is a back/forward button. Just go to the next page and disable or remove the back button. It's hard to give good advice without knowing what you are trying to accomplish.

Try something like this. Setup an array of the same length as your movieclip in frames, all initialized to false.
var framesVisited:Array = [];
// since we're talking about frame numbers here, start at 1 instead of 0 like
// you normally might.
for(var i:int = 1, ilen:int = this.totalFrames ; i <= ilen ; i++){
framesVisited[i] = false;
}
Now, each time you visit a frame, make sure its corresponding array location is set to true.
framesVisited[this.currentFrame] = true;
Now in your function to check whether a frame has been visited, just return the value of the array.
public function hasFrameBeenVisited(frameNumber:int):Boolean{
return Boolean(framesVisited[frameNumber]);
}

Related

Flash Escape Game Item

I'm making a flash game that requires picking up items. The item disappears when you click on it, but it reappears when you leave the room and come back. I tried setting a variable in the function and then using an if statement in the main code but that didn't work because the variable is local. And if I declare the variable outside the function, it reinitializes when you enter the frame(return to the room). A global variable should solve this problem. I know everyone says they're bad to use but I'm just looking for a solution.
//Pick up key
key_btn.addEventListener(MouseEvent.MOUSE_UP, getKey);
function getKey(Event: MouseEvent):void{
var gotKey:Boolean = true;
}
if(gotKey == true){
key_btn.visible = false;
}
First of all: I suggest you to keep all codes in one frame and use movieclips to represent different rooms.
You can add a layer for the global vars and expand it, such as this:

xml frame pause flash

So is it posibble to create outside source from swf file so i can control frame pause lenght(xml file or AS pacage), and edit it with notepad. I have this code for each of my frames in swf file it goes like this:
Code on my first frame goes like this:
//PAUSE
function playit(e:TimerEvent):void {
play();
}
var Tim:Timer = new Timer(100, 1);
Tim.addEventListener(TimerEvent.TIMER, playit);
stop();
Tim.delay = 100; //Adjust Accordingly - 1000 Equals 1 Second
Tim.start();
And on the rest of the frames (10 more frames) goes like this:
//PAUSE
stop();
Tim.delay = 10000; //Adjust Accordingly - 1000 Equals 1 Second
Tim.start();
Sorry to say it, but your code is a bit messy. If you are actually trying to set how much frames should be played per second (as I can see the timer), you should check frameRate property. You can read specific number from a source file and then set it with ActionScript.
You won't need all those play/stop/timer things..
Hope that's the idea, otherwise I'm speechless..
p.s.
If you still need some other kind of solution - use nextFrame/prevFrame with that timer, don't play/stop it all the time..
The solution is simple but require a bit of math. Instead of letting the movie play you need to use your own custom system based on gotoAndStop(). You calculate the right timing and then call gotoAndStop() at the right moment with a variable of int type increasing on each call.

How to save data in previous frame in Flash

I have some movieclips called one, two, three, four, five that appear on stage with addChild. I also have 4 editable text fields on stage called theText1, theText2, theText3 and theText4 where users must write the numbers 4, 1, 5, 2 in each one. If they write something wrong they are sent in a next frame where they take some feedback and come back in current frame to correct their answer. The problem is that when they come back all are "reset". Movie clips added before and numbers written before must be on stage. How can i do that? (I need something simple because i am new to flash and as3).
Because MovieClips don't store frame states.
You should store values, and restore them, after returning to the frame.
//Add these 2 helper functions in your frame with Quiz UI
function restoreValues():void{
if(this.storedValues != null){
//Restore data
theText1.text = this.storedValues.txt1;
}
}
function storeValues():void{
//Save as much data as you want
this.storedValues = {
txt1 : theText1.text
};
}
When you go to the another frame, call storeValues right before you change frame and store all necessary data in simple object.
checkQuizButton.addEventListener(MouseEvent.CLICK, onCheck);
function onCheck(e: MouseEvent):void{
storeValues();
nextFrame();
}
In the frame with Quiz ui components, just after initialisation of all ui components, call restoreValues(); Don't forget to change functions for your needs.

AS3 Global Variables

Ok so I have managed to do this once before but I simply cannot remember how or find the source I found back then. I am making a simple flash game. There are several characters moving. To make each level more difficult than the other I have decided to alter the movement speed of each character from level to level. If I declare and assign values to these variables on my main timeline in frame 1 it doesn't work the way I want it. When you go to level 2, which is in another frame, the speed goes up like it should. But when you go to the next level, which is level 1 with higher movement speed, the value assignment is processed again, which means the movement speed goes back to scratch.
//frame 1
var speed:int = 5;
//level accomplished, speed++, goto frame 2
//frame 2
//level accomplished, speed++, goto frame 1
And then all the code on frame 1 runs again, setting the value of "speed" back to 5.
I have tried putting the variable in a class, but the problem is still the same, everytime it goes back to frame 1 the code "var speed:Speed = new Speed();" runs again and the value of the variable goes back to whatever I assigned in the speed.as file.
I have tried so many different combinations to just make the global variables inside a package but outside any class so that it is always globally accessible without creating a new object.
Here is my current code:
//speed.as
package
{
public var speed:int = 5;
}
//game.fla
import speed;
trace(speed);
This throws the error "Access of possibly undefined property speed..."
As I said, I have tried many different combinations and got a lot of different errors, been googling and digging for 8-9 hours and it's driving me crazy. You guys are now my last hope, I would be very grateful for an answer easily telling me how to use my global variables (I know global variables are bad, seen a lot of people writing it even though I don't really know why), if there is another easy and better solution please do tell, but I don't want to rewrite the entire code or anything to make it compatible with some other solution. I know that global variables will solve this problem for me, I just don't know how to use them.
I will need instructions on what to do in both the .as file and the .fla file. Thanks in advance!
Does it cycle between frame 1 and 2? If it does, use functions to do it instead. You don't need the .fla.
//Declare variables
var speed:int = 5;
//In this case I use a button to transit into the frame. Edit the code as you wish.
btnLevel1.addEventListener(MouseEvent.CLICK, level1);
private function level1(e:MouseEvent):void
{
MovieClip(root).gotoAndStop(2);
speed++;
//Your other code for the level goes here
btnLevel2.addEventListener(MouseEvent.CLICK, level2);
}
private function level2(e:MouseEvent):void
{
MovieClip(root).gotoandStop(3);
speed++;
//Again, your other code goes here
btnLevel1.addEventListener(MouseEvent.CLICK, level1);
}
Should work, I think.
I think you should try changing it to
package{
public class Speed{
public static var SPEED:int = 5;
}
}
and then access it via
trace(Speed.SPEED)
But: this is very dirty :)
Ok so I found a little workaround, not global variables in a separate package, but variables that can be accessed by my entire timeline.
I simply made an extra frame with just code. In this frame I put my variables + a frameCounter variable. On my first frame I put this code:
if (frameCount == 0)
{
gotoAndStop(7);
}
So the code declaring the variables only runs once. Probably not the best solution but atleast it works as intended =)

Have images change using Flash AS3

So I have this assignment due tomorrow and Its to make an audio player in Flash using as3. I don't understand as3 at all. I have the code for the player working becuase I just used the same code we used in class, but I kind of want to make it my own.
I have created an Ipod style player.
First thing, I want the Forward button to play the next song. How would I write the code for that?
Next When a song plays I want a specific image to show up. and when the next song comes on the next image to show
This is the code i have for the songs
function playTrack(e:MouseEvent) :void {
switch(e.target.name) {
case "track1":
trackToLoad = "audio/Don't Stop Believing.mp3";
trackName = "Journey • Don't Stop Believing"
break;
case "track2":
trackToLoad = "audio/Never Never Land.mp3";
trackName = "Metallica • Never Never Land"
break;
...
but instead of having just a stop and play button and 10 buttons that play each song I want to have a skip button to go to the next song..
hope this is enough info for some help
Thanks
Set your tracks up as an array of objects:
var track1:Object = {
track: 'Don\'t stop believing',
artist: 'Journey',
file: 'dont_stop_believing.mp3'
};
var track2 //same as above
var tracks:Array = [track1, track2, ...];
You could really create a Track class, but it sounds like you aren't to that point yet.
Instead of making your playTrack function actually be the mouse event handler, you should separate it out so that it can be used universally no matter how the track begins to play (i.e. clicking on that track's button, clicking on the next button, or after the previous song ends). Write a separate function just to handle the mouse event (i.e. clickTrack()), which will call your playTrack() function.
Setting your tracks up within the array will allow you to keep note of the indices of each track (including the currentTrack) as a number. That way you can iterate through the tracks by just incrementing the currentTrack variable.
This way, you can set your playTrack() function up to take a trackNumber parameter (i.e. playTrack(1). Then just use that parameter to reference the index of the track you want to play within the tracks array. Remember that arrays are on a 0 index meaning that the first element is index [0], the second is [1], etc. So you'll either have to write your playTrack() function in that way, or convert by subtracting 1.
Try storing some kind of Track object in an indexed array. You can keep track of the index, and change it when the user clicks skip or back. The Track object could store the filepath, the track name, and the art that you want to display. When the index changes, grab the correct track from your array and update the player.
Here is an example that actually uses song titles.