Copy link to clipboard
Copied
I have been wracking my brain for a week to get this and can't figure it out. I need to have an object appear at random points on the screen each time the code is run, the stage needs to be broken down into 4 quadrants and the angle of the object changes depending on which quadrant it appears in...the stage needs to be dynamic as well...I don't need an exact answer because this is an assignment and I don't want to cheat but it's due tomorrow and I've got nothing.
Copy link to clipboard
Copied
Ok to prove I am doing this myself here's what I've got so far
package {
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
// constructor code
var hero = new Hero();
addChild(hero);
hero.x = stage.stageWidth / 2;
hero.y = stage.stageHeight / 2;
var enemy = new Enemy();
addChild(enemy);
enemy.x = Math.random() * (stage.stageWidth - enemy.width) + enemy.width/2;
enemy.y = Math.random() * (stage.stageHeight - enemy.height) + enemy.height/2;
}
}
}
And what I need to do next is:
Use the if-condition to check what quadrant each of
the enemies are in, if…
• Top right: rotate the enemy instance to 45 degrees
• Bottom right: rotate the enemy instance to 135 degrees
• Bottom left: rotate the enemy instance to 225 degrees
• Top left: rotate the enemy instance to 315 degrees
(issue is I can't find what code it is for checking the quadrant)
Copy link to clipboard
Copied
Top left quadrant is:
x < stage.stageWidth * .5 && y < stage.stageHeight * .5
Top right would then be:
x > stage.stageWidth * .5 && y < stage.stageHeight * .5
Make sense?
Copy link to clipboard
Copied
perfect I get it then I just change the >< around to get the bottoms thanks so much
Copy link to clipboard
Copied
You may find that it's conceptually easier/more maintainable to create rectangles and then use containsRect() or containsPoint() to determine what quadrant the hero is in. For example, you could create two arrays--one has the angles and one has the rectangles. Loop through the rectangles and stop where the hero is, then look up the angle in the other array.
protected var angles:Array = [0, 90, 180, 270);
protected var quadrants:Array = [];
public function Main() {
super();
var centerX:int=stage.stageWidth/2;
var centerY:int=stage.stageHeight/2;
quadrants.push(new Rectangle(0, 0, centerX, centerY));//top left
quadrants.push(new Rectangle(centerX, 0, centerX, centerY));//top right
quadrants.push(new Rectangle(0, centerY, centerX, centerY));//bottom left
quadrants.push(new Rectangle(centerX, centerY, centerX, centerY));//bottom right
}
protected function getHeroAngle(heroCenter:Point):int {
for (var i=0; i<quadrants.length; i++) {
if (Rectangle(quadrants).containsPoint(heroCenter)) return angles;
}
return -1; //indicates not found in any quadrant
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now