Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411520 Posts in 69380 Topics- by 58436 Members - Latest Member: GlitchyPSI

May 01, 2024, 02:28:11 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Pause in Flash (as3+flashdevelop)
Pages: [1]
Print
Author Topic: Pause in Flash (as3+flashdevelop)  (Read 3946 times)
davidp
Level 6
*



View Profile WWW
« on: July 28, 2010, 02:10:14 AM »

I've beeen loking around for pause command in as3, but didn't find anything really related to my problem, so I'm posting here (yet again).

I've decided to put a pause boolean in all of my children (most of them with their own enter_frame event) and pass pause boolean from "master" clip to all of it's children.

so i press a pause key and i want to set pause to all of children enter_frame events.

Code:
             ______________master____________________
            /          /            \                \
       child 1      child 2       child 3          child 4
       if !pause    if !pause      if !pause       if !pause
       enter_frame  enter_frame    enter_frame     enter_frame

is this viable? can it cause some problems that i may not be avare of?
Logged

frederiksen
Guest
« Reply #1 on: July 28, 2010, 03:22:16 AM »

i'm not an expert or even advanced programmer so don't actually take me seriously.
The way you do it shouldn't be a problem but i've read somewhere that it's not really good to use that many enter_frame events, i just don't like them because i don't know how they are queued. The way i do it now is to have just one enter_frame in the master and from there i call the update function of the child, like: child.updatefunction();
Also makes your problem easier because if you want to pause you just don't call the function.  
Logged
st33d
Guest
« Reply #2 on: July 28, 2010, 06:58:08 AM »

edit: whups ^ what he said.

PS: events in Flash are queued in the order that you add them. So if you want key events to fire before your enter frame event, you should add the key event before the enter frame event
Logged
davidp
Level 6
*



View Profile WWW
« Reply #3 on: July 28, 2010, 06:59:31 AM »

@frederiksen: your post makes sense, especially on having the master clip handle all child clips events, also pausing them all with just one if sentence. it's a lot "easier" too in sense of programming, but not readable in my opinion, that's why i tend to split stuff a much as possible - meaning including enter_frame event in child clips.

i'll have to check how many enter_frame events work on performance of the movie itself. so far i didn't have any issues with it (them), but thanks for the heads up.

@st33d: yea, you're both right. i guess i'll go and remove all child enter_frames. well actually, that would make even more sense as i already have some stuff running from my master.enter_frame > child.custom_functions and some childs with their own enter_frames.
« Last Edit: July 28, 2010, 07:02:45 AM by davidp » Logged

st33d
Guest
« Reply #4 on: July 28, 2010, 07:01:27 AM »

Not readable?  WTF

But it's how every game engine I've encountered does it.
Logged
davidp
Level 6
*



View Profile WWW
« Reply #5 on: July 28, 2010, 07:05:22 AM »

yea, i normally work in a way where i put as much as code i can to the file/object representing that... object.

i mean, if i could i'd add EVERYTHING in child events and just create them in master, but that's not possible in flash so...

yea, it's a bad idea, i already figured that out :D

no biggie, i'll just remove event listeners and run child functions from master.
Logged

Triplefox
Level 9
****



View Profile WWW
« Reply #6 on: July 28, 2010, 12:50:34 PM »

The problem is not that it's wrong to use events to drive the entities, it's that enter_frame alone does not give enough control to support a pause feature.

A simple alternative that preserves the original architecture is to create your own "my_frame" events and push them out on enter_frame, after doing the pause check. You can even split them up into various types like "player_frame" and "enemy_frame" to do some ordering of who updates first.

http://www.8bitrocket.com/2007/07/31/creating-custom-events-in-flash-as3-actionscript-3/

Performance-wise, you gain the most by entirely avoiding events and deep usage of the scenegraph, but that's not the question we're answering here. After a certain complexity level you have to use an event system anyway.
Logged

jrjellybeans
Level 3
***


They're All Gonna Laugh At You


View Profile WWW
« Reply #7 on: July 28, 2010, 02:19:00 PM »

The way that I've handled this problem is by using the Timer class.

Essentially, you have a single controlling object that updates objects and makes them do stuff. 

For example, you attached a timer to "CONTROLLER".  When the timer event is fired off, you scroll through a Vector (or Array, but Vectors are faster) of all of your objects, and then have them do what you want them to do.  That way, if you ever want to pause an object, you can just change the code in your CONTROLLER.  I tend to use a "cMove" variable for that sort of stuff:

Code:
public function onTick(e:Event):void {
   if (cMove==1) {
      var vO:MovieClip;
      for each (vO in vecPlayer) {
         vO.moveMe();
      }
      for each (vO in vecEnemy) {
         vO.moveMe();
      }
   }
   else {
      trace("The game is paused.  What should I do?");
   }
}

In fact, I don't use keyframes or timelines at all.  I don't really understand what the point of them is...

The system I use is also good for blitting.

Hope that helps.  By the way, I figured out most of this stuff from Michael Williams awesome Avoider Tutorial.
Logged

Μarkham
Level 10
*****



View Profile WWW
« Reply #8 on: July 28, 2010, 04:37:12 PM »

The way Flixel does updating is that there is the main clip that has an onEnterFrame event.  References to objects are stored in an array in the main clip.  Every frame the main clip goes through each object in the array and calls that object's Update function.  So in effect, only one object has an onEnterFrame function, and you can tell that object how to update the other objects.
Logged

BadgerManufactureInc
Guest
« Reply #9 on: July 28, 2010, 05:00:02 PM »

Most languages don't have a built in pause function as all the different ways to keep timing means there's no generic solution.

I normally create a pause boolean and bypass update if paused is true.
That way I can still render stuff if I need to.

General approach used:

Global: private var paused:Boolean;

MAINLOOP

Update:

Code:
if(paused)
{
 if(keypress[p])paused=false;
}
else
{
 if(keypress[p])paused=true;
 update();
}

Render:

if(paused)
else etc...

Hope this helps.
Logged
Glaiel-Gamer
Guest
« Reply #10 on: July 28, 2010, 07:08:13 PM »

super hacky way:
set the FPS to .01 (the minimum), that's usually "good enough" for a short flash game


(i have a much more complicated thing that involves taking control of the timeline away from flash with extensive use of movieclip.prototype, at the expense of a little extra boilerplate)
Logged
davidp
Level 6
*



View Profile WWW
« Reply #11 on: July 28, 2010, 11:45:42 PM »

super hacky way:
set the FPS to .01 (the minimum), that's usually "good enough" for a short flash game

dont_know_if_serious.jpg Ninja


anyway i went and changed all of my code to just one enter_frame event, going on in the main clip. then i just go through arrays of child clips and check if !pause then update. it's working fine. at least for now.

thanks for all the replies guys, really made me thinking and see how others do it Smiley

if anyone has another idea, just drop in a reply. i don't mind reading about other tehniques.
Logged

st33d
Guest
« Reply #12 on: July 29, 2010, 12:36:08 AM »

super hacky way:
set the FPS to .01 (the minimum)

 Cheesy

Awesome.
Logged
BadgerManufactureInc
Guest
« Reply #13 on: July 29, 2010, 07:45:02 AM »

super hacky way:
set the FPS to .01 (the minimum)
This.

Really got me laughing, dunno about thinking!

Glad the advice helped, outta start charging for this haha  Evil
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic