AS3 - Finding an objects x and y position relative to the stage - actionscript-3

I'm new to ActionScript 3 and I have a character which you can control, the screen scrolls right along the stage and he can fire missiles.
The problem I'm getting is the missiles are created via these co-ords:
bullet.x = hero.mc.x;
bullet.y = hero.mc.y
These work fine untill the screen has scrolled to the right. I assume it's because the bullets are being spawned as a result of them using the canvas x,y and not the stages x,y
So i'm wondering how to find out the x and y of my hero in relative to the canvas so i can spawn the missiles on top of him!
Thanks, and if you need any more information let me know, I'm new to all this. Thank you.

You can do that with localToGlobal and globalToLocal. Your solution would be something like:
bulletPos = bullet.parent.localToGlobal(new Point(bullet.x, bullet.y));
Beware, though, as those are last resort functions. Normally, you'd have all your elements using the same 'layer', so comparisons are easier and faster.

Related

how to create some dynamic sprites falling from top to bottom

I am making a game in cocos2dx(c++).Here i have to make dynamic sprites that are falling top to down and on touch i have to kill them.I mean the sprites are of insects and they will be killed on touch
The problem is that I am unable to get the idea on how to implement them as on increasing the level the insects will be falling more and with more speed.
I have made for one insect.But don't know how to handle for multiple insects
Suppose, your 1st Sprite is coming from y height and it fell to -y/2 with a velocity of 10.
Then, you have to make several sprites with differnt velocities and different positions of x.
You can move a sprite in cocos2dx with the help CCMoveTo()
You can use this custome method to move sprite
void GameLayer::_mov(CCSprite *cp, float x, float y)
{
cp->setPositionX(cp->getPositionX()-x);
cp->setPositionY(cp->getPositionY()-y);
}
In this method we passing sprite which we have to move and x is how much change in sprite position and y is how much change in sprite position. In this I subtract x and y from the current position of sprite but you can add also according to your requirement.
You have to call this method in your update method.

Getting graphic/movie clip x,y position from within another movieclip?

This should be fairly simple I'd think, I'm just not that familiar with actionscript haha.
I have a game where I have the background moving behind a character that stays in one position on screen. I'm relatively new to actionscript 3 but I'm wanting to have text boxes pop up whenever the player presses a key over certain objects passing in the background.
So, basically the background itself is a movie clip, and I have other graphics and movie clips within the background mc.
I was thinking of getting the player.x and y position and then "comparing" that position (>= and <=, etc.) with the graphic/movie clip in the background. But I just don't know how to obtain the x and y coordinate of the graphics/movie clips in the background mc.
You could try to target your movie clips in the background by getting their coordinates, then removing their parent's position (the background container).
Something like :
var finalXPosition:int = targetMovieClip.x - backgroundContainer.x;
var finalYPosition:int = targetMovieClip.y - backgroundContainer.y;
By substracting the target movieclip parent's position to its position, you gain the final position in the parent's scope coordinates.
It should work for you as soon as your character and your background container are situated at the same level of the display list.
Here is a quick diagram of what I try to explain (please forgive my inaptitude to draw nice and explicit drawings ^^)
Usually, when I stumble upon such a case, I try to make a quick and even dirty drawing, starting with what I want, then breaking down every useful data I have to achieve that result, you should keep that method in mind and try it the next time ! :-)

Box2D (AS3) Simulate Object absorbing force on collision?

i expect this is quite an easy one.
I'm trying to simulate a falling Bean Bag that can be knocked into a net on one side of the screen.
I have:
a dynamic body and circle shape representing a hand or bat, which moves respective to the mouse.
falling dynamic body circle shapes representing the Bean Bags.
and simply, two small circle shapes representing the net's open top.
To move the hand/bat i update a linear velocity on each step, so that this force can be applied to the bean bag:
// move hand
deltaX = (handBody.GetPosition().x * scaleF) - mouseX;
deltaY = (handBody.GetPosition().y * scaleF) - mouseY;
var newVel:b2Vec2 = new b2Vec2(-deltaX, -deltaY);
handBody.SetLinearVelocity(newVel);
My problem is that I would like the Bean Bag to absorb some of the initial force of the knock. At the moment it's too easy for the falling bags to be flung off the screen with a quick swipe. Is there a simple way to use the friction, damping or other settings? I have tried but cannot seem to create the effect. Can anyone suggest how i can remove some of the collision force manually without effecting the updated position of the bat/hand?
tia,
Chris
Can't you just multiply the new velocity/bean bag velocity with say 0.8 or whatever and call this the dampening effect?
But the provided code seems weird, I can't see anything in it related to impact or hitting a bean bag. This just sets the vel of hand?

How to track a point on rotating MovieClip?

I have a MovieClip, that is representing a character in my game. Id like to "create bullets" shooting out from the tip of my characters gun. Problem is that when my character turns around, also the point rotates around the MovieClips pivot.
Is it possible to anyhow easily track this point, so that I could dynamically create new objects at the same location.
I tried to add a new MC as a child to my character, with the initial position at the guntip. In some systems child-objects "follow" their parents around, but it didnt seem to work here.
Is there any other "native" way of doing this, or do I just have to have a Polar-coordinates representation of the point relative to character-MovieClips origin, and add the MC rotation to theta, so that I can calculate the X and Y coordinates?
Try localToGlobal() and globalToLocal() methods to transform coordinates from your character movieclip to its parent.
Set up the movie clip with the gun (I'm assuming it's at the end of an arm?) so that the gun tip is straight across from the pivot point.
Then pass the method that fires the bullet three parameters: the x and y position of the gun MC, and its current angle.
The code for your bullets initial position might look something like this:
public function CreateBullet(x,y:Number, degree:Number)
{
// set start position
this.x = x + ARMLENGTH*Math.cos((degree/180)*Math.PI);
this.y = y + ARMLENGTH*Math.sin((degree/180)*Math.PI);
}
Where ARMLENGTH is the distance from the pivot point to the end of the gun.
Two caveats, Flash can do weird things with angles, so you might have to make an if statement in CreateBullet() with inverted degrees if the player if facing backwards. Also, if you have the gun MC as a child of your character, you might have to make a Point where the pivot point is then do a localToGlobal on it. There's a good reference for that here.

localToGlobal/globalToLocal AS3 confusion

I want to move a display object from one container to another, but have it appear in the same place on screen.
I thought I'd understood this years ago, but the following does not work:
function moveToNewContainer(obj:DisplayObject, newParent:DisplayObjectContainer):void {
var pos:Point = new Point(obj.x, obj.y);
var currentParent:DisplayObjectContainer = obj.parent;
pos = currentParent.localToGlobal(pos);
currentParent.removeChild(obj);
newParent.addChild(obj);
pos = newParent.globalToLocal(pos);
obj.x = pos.x;
obj.y = pos.y;
}
This doesn't position the object in the same place as I would have expected.
Does anyone know what I am doing wrong, please?
Thanks,
James
Using localToGlobal/globalToLocal and setting the x and y properties like you showed calculates the correct position for the object in its new parent, but does not adjust for other aspects of the transformation such as scaling or rotation. In other words, the object's registration point will indeed remain in the same place, but the object may be rotated, scaled, or sheared differently.
The solution to your problem will need to take into account the transform.concatenatedMatrix properties of the old and new parents--you'll need to multiply the object's transformation matrix by one and then by the inverse of the other, or something along those lines. Leave a comment if you need help working out the math.
There is nothing wrong with your code, provided that both containers have no transformations applied. If your clips are scaled, rotated, etc.. you need to handle that in addition to the coordinate space transformations that localToGlobal and globalToLocal do.
You have to check if your containers are actually placed on stage. If your new container isn't added as a child to stage, function globalToLocal fails, just because it doesnt know how to correctly calculate that data.