A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Randomise frame event

Hybrid View

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    11

    Randomise frame event

    Hi there, does anyone know how i could change this ENTER_FRAME below event so that the function doesnt occur on every frame.

    stage.addEventListener(Event.ENTER_FRAME, snowflake);
    function snowflake (event:Event):void


    For example, it would do if it occured every 50 frames.

    Or preferably, if it could occur randomly between every 50 - 100 frames


    I'd appreciate any help at all,
    Thanks a mil
    B

  2. #2
    You can create a counting integer variable that adds every time it enters the snowflake function and a goal variable to tell the function at which frame number to initiate the code in the function, like this:
    Code:
    stage.addEventListener(Event.ENTER_FRAME, snowflake);
    //init variables
    var count:int=0;
    var goal:int=int(Math.random()*50+50);//this will give you a number from 50-100
    
    function snowflake(event:Event):void{
       if(count>=goal){
           count=0;
           goal=int(Math.random()*50+50);
           //add your code that was initially in your function here
       }
    }
    I believe this would work, there may be an error in the code, have not checked it.

  3. #3
    Junior Member
    Join Date
    May 2008
    Posts
    11

    howaya

    Thanks a mil for you help.

    Couldnt get it going though. Any chance you could have a quick look over this to see if you can spot where i went wrong. I'll understand if you dont!

    This is my initial actionscript and it works but the "snowflake" Movie appeears every frame:
    (no need to look over this unless you need to

    stage.addEventListener(Event.ENTER_FRAME, snowflake);
    function snowflake (event:Event):void
    {
    var _Snowflake:mcSnowflake;

    _Snowflake = new mcSnowflake();
    addChild(_Snowflake);
    _Snowflake.x = Math.random() * 510;
    _Snowflake.y = Math.random() * -60;


    stage.addEventListener (Event.ENTER_FRAME, snowfall);
    function snowfall(event:Event):void
    {
    if ( _Snowflake.y> 450 || _Snowflake.x> 550)
    {
    stage.removeEventListener(Event.ENTER_FRAME, snowfall);
    removeChild(_Snowflake);
    }
    else
    { _Snowflake.y += 5;}
    }
    }



    This is how i added your script:

    stage.addEventListener(Event.ENTER_FRAME, snowflake);
    //init variables
    var count:int=0;
    var goal:int=int(Math.random()*50+50);
    //this will give you a number from 50-100
    var _Snowflake:mcSnowflake; //I PUT THIS IN HERE

    function snowflake(event:Event):void{
    if(count>=goal){
    count=0;
    goal=int(Math.random()*50+50);
    //add your code that was initially in your function here


    _Snowflake = new mcSnowflake();
    addChild(_Snowflake);
    _Snowflake.x = Math.random() * 510;
    _Snowflake.y = Math.random() * -60;



    stage.addEventListener (Event.ENTER_FRAME, snowfall);
    function snowfall(event:Event):void
    {
    if ( _Snowflake.y> 450 || _Snowflake.x> 550)
    {
    stage.removeEventListener(Event.ENTER_FRAME, snowfall);
    removeChild(_Snowflake);
    }
    else
    { _Snowflake.y += 5;}
    }
    }
    }

    Any thoughts on where i'm gone wrong,
    no error messages come up but nothing happens when you test movie

    Thanks again for your help

  4. #4
    What exactly do you want happening with the flash file you want?

    I mean what effect do you want. Do you want to create a snowflake every 50-100 frames, have it fall then make it disappear after a certain point?

    Do you have a fla file you can post here which I can look at?

    Thanks,

    TariqM

  5. #5
    Junior Member
    Join Date
    May 2008
    Posts
    11

    howaya

    I am making a game for college and have been trying to get this right for ages.
    "snowflake" is actually a coin! I recycled actionscript from another project, so left the name "snowflake" on the coin movie.

    Basically the coins, fall from the sky at random and a little character at the bottom moves left and right and tries to catch them. I intend to use a hit test and dynamic text to count the coins he catches and then he flies off to piggy heaven

    Sounds easy enough, but have been at it for days now!!

    I attached two fla,
    in piggy2.fla , i got piggy moving
    in the 'lots of coins.fla', I have coins falling every frame instead of every now and then as you would expect in a game

    Thanks
    Attached Files Attached Files

  6. #6
    Can't believe I forgot this, but I forgot to increment the count variable...

    Just add "count++;" as the first statement the snowflake function calls.


    I attached a fix version of what you want.

    Sorry about that
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center