How to Create Boundaries in AS3 - actionscript-3

I am new to AS3 and I wanted to know how to make a frame Boundary in flash, I am making a platformer game. My code Logic is like :
Whenever I move with ArrowKeys Move Background invert, so if I click Left key the background will move Left. so this is not really what I want as my Player (moving avatar) will always be stick to the middle of the stage. How to make a New boundary so whenever Player go near Right end of Stage ... Background should go left as much x moved outside boundary of player.
more illustration :
http://i.imgur.com/HOw6vHI.png

if(player.x > rightBound){
map.x += rightBound - player.x;
player.x = rightBound;
}
Repeat for the other 3 sides.

Related

AS3 - how to stop moving stage if it reaches the edge of background

I'm using a sidescroller where the stage follows the player until the background reachs the edge.
But how do I make it so the background doesn't show the stage when touching the end of the background? Like when it reaches the near end, the stage needs to stop moving and the player needs to move to the end on it's own. That way the stage won't show when touching the edge of the sky.
I currently have a code for the stage following player, but confused on how to make the stage stop at the end or beginning of map.
x = -(player.x-stage.stageWidth/2)
Maybe something like this.
x = -(player.x-stage.stageWidth/2);
if (x < 0){
x = 0;
} else if(x > stage.stageWidth){
x = stage.stageWidth;
}
Or whatever values you will need to check as your edge cases.

Collision Detection with ActionScript 3.0

Hey I am beginner Flash Action Script 3 developer.
I am using hitTestPoint() to detect collision between a car and a stage drawing. Car is moving in the stage so I am using hitTestPoint().
There is a problem, Lets say.
Car is a square, it is actually a perfect square right now.
I am doing this:
heightHalf = car.height / 2;
widthHalf = car.width / 2;
if(level.hitTestPoint(car.x + widthHalf, car.y + heightHalf,true)){
trace( "Right Collision" );
}
It should work as, car.x + the half of its with should return the point on x-axis which is colliding and same with the y-axis. But its not working.
When my car collides with the right walls it doesn't produce error or trace, but If I move my car further out of stage(as car can go through walls) just before it can completely move out, it produces trace error just when left side is colliding with walls.
These pics should help:
Right Collision with no error: http://i.minus.com/ibqvrbNHuLTTIX.png
Error but with wrong side: http://i.minus.com/iGRNRVmCwwY4x.png
Inverting the + - signs isn't helping either.
Take notice to where the anchor point is on the car object. since you are using Flash IDE, The anchor point could be in the middle, right, or left corner.
Also you will need multiple points do to this type of hitTesting. at least one for each side of car.
If your registration point is top left, then you are hitTesting the middle point of your car. So it will only register collision when half of the car goes over the wall.
Check your registration point. When you create a new movieClip or Sprite you can select the registration point by clicking one of the 9 box on the square that comes up under the name of the Object.

I am trying to make 2 circles around a point and fill between these points and listen for a click between the circles

i'm trying to make something for a game i'm making. When someone clicks on the movieclip i want it to draw an inner circle and an outer circle. I'd like to fill between the circles with a opaque colour (purple in the image) so people can see basically a large thick circle around the movieclip but not touching the movieclip. I then need to check if the mouseclick happens between the two circles only.
The image below shows what i mean. The thing is the thickness of the purple bit has to be adjustable (not in game as such), if you click on 1 movieclip the thickness of the purple bit may be 10pixels, a different clip may be 50. Obviously checking for a click greater than inner circle x and less than outer circle only works on a straight line across from the clip, once you move up or down this doesn't work so well. Any help is much appreciated as i can't seem to work this out. I've tried drawing 2 circles and also tried using 2 movieclip circles but can't get it to work.
Seems i cant upload pictures on here. Easiest way is to think of a no entry sign without the / line going through the middle. The centre is the movieclip, the inside part of the red circle the inner circle and the outer is the outer circle but at no point does the red touch the movieclip
I would measure the distance from the center of the circles to the mouse click point. Then you just need to check is that distance greater than the inner circle radius and less than the outer circle radius.
Something along these lines:
var clickPoint:Point = new Point(mouseX, mouseY);
var centerPoint:Point = new Point(circleMC.x circleMC.y);
var dist:Number = Point.distance(clickPoint, centerPoint);
if(dist > innerRadius && dist < outerRadius){
trace("the click happened between circles
}

Flip character horizontally (top lef registration point)

I have created Mario character, it is playing different animation like standing, walking, jumping...Everything is ok but flip horizontally.
In my key_down function I set if rightKey is pressed player.scaleX = 1,
else leftKey is pressed player.scaleX = -1;
It works, my english is not so good so here is the link to understand better :
http://www.fastswf.com/vsi-Wps
The only problem is that even if you touch only one time left or right key,
it will flip player but also it will move the player about 30px (lets say).
I used this before and it works ok if player has center registration point, but now I have top-left registration point and dont know how to fix this?
Can anyone explain?
After scaling by a factor of -1, you have to move your DisplayObject to the right by width number of pixels. Here's an image illustrating what I mean. The orange dot in the image is the registration point.
And here's what you need to add to the code:
player.scaleX = -1;
player.x += width;

how to make walls in actionscript 3.0?

i've been making a twist on the labyrinth game and i've got my ball to move with physics but im struggling with getting it to hit the walls around it. its currently a movie clip with black walls, and ive used this code to try and stop it:
if (character.hitTestObject(walls)){
character.x = //something
character.y = //something
}
all this does is when it hits any part of the movie clip, (even the blank spaces) it moves my character,
is there any sort of code i can use to maybe detect hitting a certain colour?
One way you could do this, is to use hitTestPoint() method to test if any of the corners have hit your wall.
hitTestPoint() tests only a single location to see if that point collides with an object. This is how you could test the top left corner of your character to see if it's touching the wall :
// I am assuming that x,y is the top left corner of your character
if (wall.hitPointTest(character.x, character.y, true))
{
// top left collided with wall
{
So you could do the same for all corners, or if you want, you can determine any collision points you want to check for the character.
Depending on your level of precision, this method might work just fine for your needs. But if you want pixel perfect collision, you can check out this link :
http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/