-
Why is my stroke setting affecting my bounce detection code?
I generate a bunch of "enemies" (Class "Enemy") that bounce randomly around the screen. The Enemy Class symbol is simply a square. The function moveEnemy moves the Enemy. If the Enemy's edge goes beyond any of the background square's edges, the enemy's speedX or speedY is inverted, so it reverses direction. (The Enemy Class has speedX and speedY variables.)
Here's what's confusing me: If the Enemy symbol has no stroke, it bounces at the right time -- just as its edge hits a border. If I add a stroke, it also seems okay as long as the stroke Scale is set to Normal. If stroke Scale is set to None, then the Enemies reverse direction well before they hit the edge (and the bigger the Enemy, the farther away it bounces). It seems as though the object acts as if its stroke was actually scaling, so it bounces when it would have hit the edge if the stroke had scaled.
Is this what's happening? Is it a Flash bug? Is there any way to have a non-scaling stroke that won't have this problem?
Thanks!
Code:
package {
import flash.display.MovieClip;
import flash.events.*;
public class Keepaway extends MovieClip {
private var numEnemies:int = 15; //this is the number of enemies
private var sizeIncrease:Number = 10; // how much bigger to make each enemy. bigger number = bigger scale
private var enemyX:Number = stage.stageWidth/2; // where enemies start
private var enemyY:Number = stage.stageHeight/2; // where enemies start
private var speedMultiplier:Number = 10; // higher = faster
private var speedMin:Number = 5; // this is the minimum speed allowed
public function Keepaway() {
var theBackground:Background = new Background; //cover the stage with a background rectangle
theBackground.width = stage.stageWidth;
theBackground.height = stage.stageHeight;
addChild(theBackground);
//add some Enemy symbols and give them increasing sizes
for (var i:int = 0; i < numEnemies; i++) {
var enemy:MovieClip = new Enemy;
enemy.x = enemyX;
enemy.y = enemyY;
enemy.width += i*sizeIncrease; // make each enemy a little larger
enemy.height += i*sizeIncrease; // make each enemy a little larger
enemy.addEventListener(Event.ENTER_FRAME, moveEnemy, false, 0, true)
addChildAt(enemy, 1);
}
function moveEnemy(evt:Event){ //give each enemy a random motion and bounce if they hit the edge of the stage
evt.target.x += (evt.target.speedX) * speedMultiplier;
evt.target.y += (evt.target.speedY) * speedMultiplier;
if (evt.target.x - evt.target.width/2 <= theBackground.x){ //too far left
evt.target.x = theBackground.x + evt.target.width/2;
evt.target.speedX *= -1;
}
if (evt.target.x + evt.target.width/2 >= theBackground.width){ //too far right
evt.target.x = theBackground.width - evt.target.width/2;
evt.target.speedX *= -1;
}
if (evt.target.y - evt.target.height/2 <= theBackground.y){ //too far up
evt.target.y = theBackground.y + evt.target.height/2;
evt.target.speedY *= -1;
}
if (evt.target.y + evt.target.height/2 >= theBackground.height){ //too far down
evt.target.y = theBackground.height - evt.target.height/2;
evt.target.speedY *= -1;
}
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|