Flip character horizontally (top lef registration point) - actionscript-3

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;

Related

detect collision only on top of the object in box2d & cocos2dx

I am creating a game like bounce ball using cocos2d-x and box2d. I have different objects like rectangle, square etc.. I am able to detect collision, but i want to detect collision only on top of the objects. What exactly i want is, when the ball is on the top of the object, then only i want to jump the ball.
But, when the ball is collide on remaining side(bottom or left or right) i don't want to jump the ball.
In touchbegan, i am using the following code to the bounce ball. So every touch it is jumping when it collide with remaining side.
if(_ball->boundingBox().intersectsRect(rect->boundingBox()))
{
b2Vec2 force = b2Vec2(0, 550);
_body->ApplyLinearImpulse(force, _body->GetPosition());
}
Any advice?
Following steps can solve your problem-
1) CCRect projectileRect = CCRect(float x, float y, float width, float height);
if(_ball->boundingBox().intersectsRect(projectileRect))
{
b2Vec2 force = b2Vec2(0, 550);
_body->ApplyLinearImpulse(force, _body->GetPosition());
}
2) - Make body of an object and then check their collision.
I got the solution for my question from the below link.
http://www.raywenderlich.com/28606/how-to-create-a-breakout-game-with-box2d-and-cocos2d-2-x-tutorial-part-2

Destroying an object on the stage issue

I am trying to create a simple Tower Defense Shooting game via the help of an online tutorial. The tutorial doesn't address an issue though.
This code is supposed to remove a bullet fired once it leaves the stage, but the bullet is only being removed upon leaving through the top or left sides of the stage.
My understanding is that the stageWidth/Height are supposed to handle the top and left and the <0 handles the bottom and right. I cannot see anything that would be an issue. Can anyone see why it isn't working for the bottom or right sides of the stage?
if (bullet.x < 0 || bullet.x > stage.stageWidth || bullet.y < 0 || bullet.y > stage.stageHeight){
bullet.removeEventListener(Event.ENTER_FRAME, moveBullet);
bullet.parent.removeChild(bullet);
bullet = null;
}
the bullet's coordinate system isn't necessarily the same as the stage's-- are they attached to the stage or maybe some other movieClip?
I'd suggest debugging, or adding trace statements, to see what the bullet's coordinates, and the stageWidth and Height are in the above code:
trace("bullet.x="+bullet.x+", stage.stageWidth="+stage.stageWidth);

How to Create Boundaries in AS3

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.

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/

Reset/lock html5 canvas position to center on player when panning

I cannot figure out how to do this. I was translating the character and background at the same time, but if there's any hiccup, the character position slides out of the viewable area of the canvas, and I need the canvas translation to be based off the position of the player (hero.x, hero.y).
Currently I have
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 640;
canvas.height = 480;
//then in my update function
if (38 in keysDown && hero.y > 0){ //UP KEY PRESSED KEY
ctx.translate(0,12); //translate background position +12y
hero.y -= hero.speed * modifier; //move player up on the background image
else if (40 in keysDown && hero.y < 3750-64){ //DOWN KEY PRESSED
ctx.translate(0, -12);
hero.y += hero.speed * modifier;
}
}
That moves the player and the canvas but not guaranteed together...if it freezes at all, the player is off center or even off screen.
The viewable canvas area is 640x480, but the background image you can navigate on is 5,000 x 3750.
On the web browser, when it doesn't freeze, it works how I want, moving the player and background at the same pace as the character.
However, that same rate on the phone puts the player much faster than the screen translates which means the player walks right out of the viewable area even though it still moves the background.
If I do ctx.translate(hero.x, hero.y) and use the hero.x, hero.y coordinates of the player, or some variation of it minus an offset, it moves the background BY that measurement each time I press the key instead of moving it TO that position.
How can I make everything conditional on the players position to move both the player and background, but together, or automatically adjust next update() to center on the player....?????
How can I make everything conditional on the players position to move both the player and background, but together, or automatically adjust next update() to center on the player
Well, the easy way would be to actually always draw the player in the center! In other words, never ever change or translate his or her coordinates. Instead worrying about translating everything else in relation to that.
Since you want the player to always be in the center, you should always draw the player at the center of the canvas (640/2 x 480/2 for you)
Then you'll want to do is keep a canvas offset for X and Y and draw everything (Background, etc) using that offset, then reset the transformation and draw the hero in the plain old center.
So your draw function might look something like this:
function draw() {
ctx.clearRect(0,0,500,500);
// Save the default transformation matrix
ctx.save();
// Translate by the background's offset
ctx.translate(xoffset, yoffset);
// Draw the background (and maybe other things) translated
ctx.fillStyle = backgroundGradient;
ctx.fillRect(0,0,500,500);
// We restore to the default transformation
// This will draw the hero not translated at all
// That means we can always draw the hero in the center!
ctx.restore();
// hero is a black rectangle
ctx.fillRect(240, 240, 20, 20);
}
Here is a live example that works with mouse down and up. There is a big "sun" for the background that moves, while the "stays" rectangle stays still because it is literally always drawn in the same place:
http://jsfiddle.net/3CfFE/