A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: How to spawn a movieClip somewhere in the stage area?

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Posts
    107

    Talking How to spawn a movieClip somewhere in the stage area?

    Hi,

    I have a mini-game where I have three movieclips:

    The player (controlled by mouse);
    A random object that moves around the stage (like the arkanoid ball without the paddle and the blocks);
    A simple power up.

    The game is simple: hitting the power up increases your score. The game ends when the object hits the player.

    The question is:

    How can I spawn the power-up (randomly) on the stage area, every time the player hits it?

    When that happens I want the following events to occur:

    1) The power-up to be removed;
    2) Increase the score by 1.
    3) Place a new power up randomly on the stage.
    4) Rinse and repeat.

    Unfortunately, I don't know how to build a function that does that.
    Which is the better way? Using a for loop to place the power ups?

    Thanks.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Use a mouseMove or enterFrame on the stage so you can run a function every time the player is moved. When that happens, use a hitTest to see if the player has hit the power up - and if so just move the powerup to a new position (stageWidth * math.random, stageHeight * math.random) - then add one to the score.

  3. #3
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Thanks for the help.

    Yah, that part I knew, but I'm a bit confused with the code iteself because in this game, I'm placing all the code in frames.

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Moving across frames shouldn't be an issue unless objects are being destroyed and recreated or you're running code at different times - is there something specific that's breaking? Can you post some code?

  5. #5
    http://pat.theorigin.net cresquin's Avatar
    Join Date
    Jun 2003
    Location
    Los Angeles, CA
    Posts
    685
    place all of your code in frame one. if you want the code to execute at some time other than frame one, put it in a function on frame one and make a call to the function.

  6. #6
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Thanks for the help.

    Maybe I wasn't clear. I meant I´m not using class files (I know I should), therefore I'm placing the code on frames, which makes the code a little bit different.

    I have been learning to code with .as files but for this project I wanted to try to code using the frames.

    This is what I have on my "play" frame. There's a "var mainScore:int = 0;" but I haven't implemented it yet.

    PHP Code:
    stop();

    // VARIABLES

    var mainScore:int 0;

    var 
    ballXSpeed:Number = -8;
    var 
    ballYSpeed:Number = -8;

    var 
    starXSpeed:Number 8;
    var 
    starYSpeed:Number 8;

    var 
    leftKeyDown:Boolean false;
    var 
    upKeyDown:Boolean false;
    var 
    rightKeyDown:Boolean false;
    var 
    downKeyDown:Boolean false;

    var 
    manSpeed:Number 7;

    // MOVE RED CIRCLE

    mcBall.addEventListener(Event.ENTER_FRAMEmoveBall);

    function 
    moveBall(event:Event):void
    {    
        
    mcBall.+= ballXSpeed;
        
    mcBall.+= ballYSpeed;
        
        if(
    mcBall.>= stage.stageWidth-mcBall.width)
        {
            
    ballXSpeed *= -1;
        }
        
        if(
    mcBall.0)
        {
            
    ballXSpeed *= -1;
        }
        
        if(
    mcBall.>= stage.stageHeight-mcBall.height)
        {
            
    ballYSpeed *= -1;
        }
        
        if(
    mcBall.<0)
        {
            
    ballYSpeed *= -1;
        }    
    }


    // MOVE STAR

    mcStar.addEventListener(Event.ENTER_FRAMEmoveStar);

    function 
    moveStar(event:Event):void
    {    
        
    mcStar.+= starXSpeed;
        
    mcStar.+= starYSpeed;
        
        if(
    mcStar.>= stage.stageWidth-mcStar.width)
        {
            
    starXSpeed *= -1;
        }
        
        if(
    mcStar.0)
        {
            
    starXSpeed *= -1;
        }
        
        if(
    mcStar.>= stage.stageHeight-mcStar.height)
        {
            
    starYSpeed *= -1;
        }
        
        if(
    mcStar.<0)
        {
            
    starYSpeed *= -1;
        }
        
    }

    man.addEventListener(Event.ENTER_FRAMEmoveMan)

    // MOVE PLAYER

    function moveMan(event:Event):void
    {
        
    Mouse.hide();
        
    man.mouseX;
        
    man.mouseY;
    }


    // COLLISION CIRLE AND MAN

    stage.addEventListener(Event.ENTER_FRAMEcircleHits);

    function 
    circleHits(event:Event):void
    {
        if(
    mcBall.hitTestObject(man))
        {
            
    man.removeEventListener(Event.ENTER_FRAMEmoveMan)
            
    stage.removeEventListener(Event.ENTER_FRAMEcircleHits);
            
    mcStar.removeEventListener(Event.ENTER_FRAMEmoveStar);
            
    mcBall.removeEventListener(Event.ENTER_FRAMEmoveBall);
            
            
    gotoAndStop("gameOver");
        }

    Maybe I should do two functions. One for the collision and one for the power up creation and placement? What would you do?

    Thanks again.
    Last edited by Hurdarr; 05-30-2009 at 09:06 PM.

  7. #7
    http://pat.theorigin.net cresquin's Avatar
    Join Date
    Jun 2003
    Location
    Los Angeles, CA
    Posts
    685
    There is really very little difference using code in frames and code in classes. What is the problem with that code?

  8. #8
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Yes, but there some subtle differences.

    The collision part is done but I'm having some problems with the second, creating a function that does (when the player hits the power up):

    1) The power-up to be removed;
    2) Increase the score by 1.
    3) Place a new power up randomly on the stage.
    4) Rinse and repeat.

  9. #9
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    You should be able to just drop this into your moveMan function:

    PHP Code:
        if(mcStar.hitTestObject(man)){
            
    mainScore++;
            
            while(
    mcStar.hitTestObject(man)){
                
    mcStar.Math.random() * stage.stageWidth;
                
    mcStar.Math.random() * stage.stageHeight;            
            }
        } 

  10. #10
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Edit:Nevermind, I was coding like I was using classes.

    Thanks for the help

    Another quick question.

    Everytime the player collides with the star, I want the ball to move gradually faster.

    PHP Code:
    function moveMan(event:Event):void
    {
        
    Mouse.hide();
        
    man.mouseX;
        
    man.mouseY;
        
        if(
    mcStar.hitTestObject(man)){ 
            
             
            while(
    mcStar.hitTestObject(man))
            { 
                
    mcStar.Math.random() * stage.stageWidth
                
    mcStar.Math.random() * stage.stageHeight
                
    gameScore++;
                
    gameScoreField.text String(gameScore);
                
    ballXSpeed += 1;
                
    ballYSpeed += 1;
            } 
        } 

    Oddly, sometimes the ball moves slower. Anyone knows why?
    When I increase the values from 1 to 5, the ball does go faster sometimes (and slows) but also does some weird twists.

    Finally, in addition of making the ball gradually faster, I also and to make it gradually bigger. Which property do I use? Scale?

    Thanks.
    Last edited by Hurdarr; 05-31-2009 at 06:14 PM.

  11. #11
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Hrmm - your ball will slow down if it's already moving in a negative direction (eg. - if speed is -7, adding 1 gives you -6) - you could use a conditional to test:

    PHP Code:
    ballXSpeed += (ballXSpeed 1) ? : -1
    And yeah - you can just add to scaleX and scaleY to make the ball bigger (you could just use *= 1.25 for +25%).

  12. #12
    http://pat.theorigin.net cresquin's Avatar
    Join Date
    Jun 2003
    Location
    Los Angeles, CA
    Posts
    685
    you could also store velocity as angle and speed rather than x and y speeds. That way when you add 1 to speed it always speeds up without the conditional.

    while this does get into some trig, it also makes the physics of your game much more straightforward.

  13. #13
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Thanks for the input guys.

    Hrmm - your ball will slow down if it's already moving in a negative direction (eg. - if speed is -7, adding 1 gives you -6)
    I spent almost an hour trying to find out why the ball moved slower. I totally forgot I had negative speed. Of course the ball goes slower when the value approaches 0.

    Mental note: don't code when you're tired.

    Hmm...that conditional code, that's a ternary operator right? I must do a bit of research first, that's new stuff for me.

    Thanks for the help.

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