PDA

View Full Version : [AS3] Collisions


actionScripter1
12-09-2008, 09:41 AM
Hi, I am trying to do a tetris with Flex, using ActionScript 3 and I am having some problems with the collisions of the blocks... Collisions are working, but blocks collide on their corners, which is a problem and I do not know how to solve this... Any ideas?

Also, I am trying to get the global coordinates of my blocks, to solve that problem, but for some reason I never get the global coordinates... This is what I am doing to get them (each block is inside an array of Sprites). The array is named squares. The example below shows how I try to get the global x and y points for a T-block:

var pt1:Point = new Point( (squares[counter-3].x), (squares[counter-3].y));
pt1 = squares[counter-3].localToGlobal(pt1);
var pt2:Point = new Point( (squares[counter-2].x) , (squares[counter-2].y));
pt2 = squares[counter-2].localToGlobal(pt2);
var pt3:Point = new Point(squares[counter-1].x, squares[counter-1].y);
pt3 = squares[counter-1].localToGlobal(pt3);
var pt4:Point = new Point(squares[counter].x, squares[counter].y);
pt4 = squares[counter].localToGlobal(pt4);

When I trace that, I get the same positions for every square, while in fact they are definitely in different positions in the stage...

Any ideas?

Thanks!

newblack
12-09-2008, 03:21 PM
the reason you're getting the expected values is because you're transforming each square's position, which is relative to its parent (presumably the same for each). but you're trying to work with the coordinate frame of each square itself, so to get the world space position of each, you pass the point (0,0). a potentially faster way of computing this is to take the tx and ty values of each square's concatenated transformation matrix. another way to do it is to use the squares' parent's localToGlobal with each square's position.

actionScripter1
12-10-2008, 08:27 AM
But how would I do it? I am testing now with the following code:

var piso:Sprite = new Sprite();
piso.graphics.beginFill(0xFF);
piso.graphics.drawRect(stage2.x, stage2.y + stage2.height - marcoPanel, stage2.width, 1);
piso.graphics.endFill();
this.rawChildren.addChild(piso);

//Now I want to get the global coordinates:
var pt5:Point = new Point();
pt5 = piso.localToGlobal(new Point(piso.x, piso.y));

However, it is not working... I also tried:

var pt5:Point = new Point(this.piso.x, this.piso.y);
pt5 = this.piso.localToGlobal(pt5);

with no success, after a trace to pt5, I keep getting (x=0, y=0)... How can I get the global coordinates of that sprite?

Thanks!