A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Randomizing Symbol Movement within a range

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    5

    Randomizing Symbol Movement within a range

    Hey,

    Basically, I've got a small space shooter game and the idea is to have the enemies start moving at the top of the screen and head towards the player, while growing in scale, and moving at an angle.(To give the sort of perspective/distance effect). Right now, I'm able to get the enemies to move at one angle only. How would I get them to move on X at a certain range?

    This is the code affecting movement/Scale
    Code:
    			for( i = 0; i < enemyShip.length; i++ )
    			{
    				enemyShip[i].y += 10;
    				enemyShip[i].x += 5;
    				enemyShip[i].scaleX += 0.03;
    				enemyShip[i].scaleY += 0.03;
    			}


    The pic above basically shows what I mean by the perspective look. I need to blocks to move towards the player at various X ranges within those lines, but I don't want them to zigzag while moving.

    I know it has to do with Math.Random, but I'm having trouble understanding how to use it in a situation such as this.

    Any help will be greatly appreciated. If you need to see more code, let me know.
    Last edited by couto607; 04-07-2010 at 06:11 PM.

  2. #2
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    This is how I would do it, which might be a little more complicated than what you're used to, but I'll step you through it.

    For starters, I'd attach the size of the object to it's y position. This is completely wrong if you were doing actual 3D, but since you're just faking it, and the bottom of the screen is "closer", we can get away with it.

    Code:
    enemyShip[i].scaleX = enemyShip[i].scaleY = 1 + enemyShip[i].y / stage.stageHeight;
    These are just sample values... play around with the numbers to get it so it works for you. From my example, objects would be scaled 100% at the top of the screen, and 200% at the bottom.

    Next, instead of manual setting x and y velocities, we're going to use speed and direction. This will allow us to keep the objects moving in a straight line, AND at a consistent speed.

    BTW, if you don't know about radians, read up on them here. Flash (and every other programming language ever) uses radians for all math functions.

    Now, what we want to do is create a directional range, where the objects will move in a random direction between two values.

    Code:
    var direction:Number = Math.PI/2;
    var range:Number = Math.PI/8;
    var finalDir:Number = direction + (Math.random() * range * 2) - range;
    Here's what's going on... (I'll talk in degrees since it makes more sense, but understand all the code is in radians.) We've got our base direction (straight down, or PI/2), and a range of 22.5 degrees (PI/8) in either direction. To build the "random" factor, we double the range, because it's 45 total degrees we're going to be picking between. The Math.random() function returns a value between 0 and 1, so multiplying that by 45 degrees will produce a value between 0 and 45. We then subtract the range from that result, which will give us a value between -22.5 and 22.5 degrees. Finally, we add this back to the original direction we were pointed in. Make sense? Probably not, so read it again. :P

    So now we've got our direction in radians. What we need to do is convert this into a unit vector so we know our x and y components. This is actually pretty easy, if you know trig.

    Code:
    var xDir:Number = Math.cos(finalDir);
    var yDir:Number = Math.sin(finalDir);
    The final component is the easiest part... including the speed. It's just a straightforward multiplication.

    Code:
    var speed:Number = 5;
    var xDir:Number = speed * Math.cos(finalDir);
    var yDir:Number = speed *Math.sin(finalDir);
    And then we just add these values to the current position. So your final code should look like this:

    Code:
    for( i = 0; i < enemyShip.length; i++ )
    {
    	var direction:Number = Math.PI/2;
    	var range:Number = Math.PI/8;
    	var finalDir:Number = direction + (Math.random() * range * 2) - range;
    
    	var speed:Number = 5;
    	var xDir:Number = speed * Math.cos(finalDir);
    	var yDir:Number = speed *Math.sin(finalDir);
    
    	enemyShip[i].x += xDir;
    	enemyShip[i].y += yDir;
    	enemyShip[i].scaleX = enemyShip[i].scaleY = 1 + enemyShip[i].y / stage.stageHeight;
    }
    Voila! You can now even randomize the speed so objects move fast or slow.

    Hopefully that compiles okay... I actually just started learning AS3 about 3 days ago, so I'm still getting used to the language. (I've got a bunch of other programming experience though, as you might have guessed.)

    If you've got any questions or are confused about anything, just let me know.
    Last edited by marshdabeachy; 04-08-2010 at 12:28 AM.

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    5
    Hey, really appreciated the reply. I understand what you're doing, but what I noticed is that it only randomizes each time I compile, and it remains consistant while i'm playing. That's not exactly what I'm loking for. I need it to randomize during play.

    If it helps, here's the array which spawns the symbol
    Code:
    			fireTimer = new Timer(500, 1);
    			fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
    			if (drop)
    			{
    				enemyShip.push( new Enemy() ); //creates the image
    				addChild( enemyShip[enemyShip.length - 1] ); //adds image to stage
    				//enemyShip[enemyShip.length - 1].x = Math.round(Math.random()*300)
    				enemyShip[enemyShip.length - 1].x = Math.round(Math.random()*(450-400)+400)
    				enemyShip[enemyShip.length - 1].y = 0
    				drop = false
    				fireTimer.start();
    			}
    Thanks.

    EDIT: Nevermind, I've managed to achieve what I wanted just by duplicating the arrays and changing the direction of each spawning projectile.
    Last edited by couto607; 04-08-2010 at 05:24 PM.

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