A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Randomise frame event

  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

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

    howaya

    Thanks so much. That is perfect. Fair play to you.

    Now I can motor on with trying to put the rest of it together. I had spent ages trying to sort that out.

    Thanks again,
    b

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

    hi

    hi there,

    You helped me so well the last time, i have one last question for you about my project, if you could PLEASE help me again.


    I've been at it since to no avail though. It wrecking my head!

    In the fla (attached) the piggy is on stage and i'm using a hittest for with the random coins which arent on stage, so i think im supposed to use the variable name, but i cant get it to work.

    I attached an example zipped fla if you could take a look i'd be very happy
    snowflake is a movie clip of a coin falling, Piggy is a pig

    Really sorry for bothering you,
    This is part of a big an assignment i have to have in for friday and ive hit a brick wall.
    Thanks for your help
    B




    The code i used is as follows in case you dont want to look at the fla:



    // initialize arrow variables
    var leftArrow:Boolean = false;
    var rightArrow:Boolean = false;
    var upArrow:Boolean = false;
    var downArrow:Boolean = false;

    // set event listeners
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
    stage.addEventListener(Event.ENTER_FRAME, moveMascot);

    // set arrow variables to true
    function keyPressedDown(event:KeyboardEvent) {
    if (event.keyCode == 37) {
    leftArrow = true;
    } else if (event.keyCode == 39) {
    rightArrow = true;
    }
    }

    // set arrow variables to false
    function keyPressedUp(event:KeyboardEvent) {
    if (event.keyCode == 37) {
    leftArrow = false;
    } else if (event.keyCode == 39) {
    rightArrow = false;
    }
    }

    // move every frame
    function moveMascot(event:Event) {
    var speed:Number = 5;

    if (leftArrow) {
    piggy_mc.x -= speed;
    }
    if (rightArrow) {
    piggy_mc.x += speed;
    }
    if (upArrow) {
    piggy_mc.y -= speed;
    }
    if (downArrow) {
    piggy_mc.y += speed;
    }
    }


    stage.addEventListener(Event.ENTER_FRAME, snowflake);
    var count:int=0;
    var goal:int=int(Math.random()*50+50);

    function snowflake (event:Event):void
    {
    count++;
    if(count>=goal){
    count=0;
    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);



    //HERE IS THE HITTEST, I USED THE VARIABLE _Snowflake FROM ABOVE

    _Snowflake.addEventListener (Event.ENTER_FRAME, enter_frame) ;
    function enter_frame (event:Event) :void{
    if (_Snowflake.hitTestObject(piggy_mc)) {
    trace ("piggy got coin")
    }
    }



    removeChild(_Snowflake);
    }
    else
    {
    _Snowflake.y += 5;
    }
    }
    }
    Attached Files Attached Files

  9. #9
    I really suggest you look over this code, it looks as if your just trying to take examples and put them together. You should learn how this code works and why it works. Also try stuff from scratch, if I'm wrong though I apologize.

    What I'm doing here is just adding another if statement to each snowflakes individual EnterFrameFunction event handler function. A simple hitTestObject method works fine in your case.

    Please review it and ask questions if you don't understand how this works.
    Attached Files Attached Files

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

    hi

    Thanks so much. No you are completely right. I am doing a course in Multimedia and have a flash module, but it doesnt entail much actionscript. The assignment were given was just supposed to be about making something with an "emotional core" like a short animation but we decided to challenge overselves and having invested so much time we cant start again with the deadline looming. I had hit an impasse.

    I will reference this post on submission.
    Thank you very much for your help, i will study that code and appreciate your offer to answer my questions.

    Thanks again,
    brian

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