Hi. I have a Formes.hx class for creating rectangles. How can I make in my Main.hx class, when I create a new type of object shapes, that posX, posY, dimL and dimH is stored in that Array.
package; import openfl.display.*; import openfl.events.*; import openfl.geom.*; class Formes extends Sprite {
public function new(posX:Int, posY:Int, dimL:Int, dimH:Int)
{
super();
graphics.beginFill(Std.int (Math.random () * 0xFFFFFF));
graphics.drawRect(0, 0, dimL, dimH);
x = posX;
y = posY;
}}
Thanks Blufedora for your help. But I have another problem. I need to do collision between a Player and Rectangle in formArray.
That is to say that I want to compare the x, y coordinates of my player with the x, y coordinates of the objects in the table so that if the coordinates of my player with a corresponding coordinate in the table so my player is in collision with one of the triangles.
for (form in formArray)
{
if(withinRange(player.x, form.x, form.x + form.width))
{
trace("player x is inside the rectangle");
}
}
function winthinRange(val:Float, min:Float, max:Float):Bool
{
return val > min && val < max;
}
public function topColliding(collider:Formes):Bool
{
if (collider.bottom >= this.top && collider.bottom < this.bottom) return true;
return false;
}
public function rightColliding(collider:Formes):Bool
{
if (collider.left >= this.right && collider.left < this.left) return true;
return false;
}
public function leftColliding(collider:Formes):Bool
{
if (collider.right >= this.left && collider.right < this.right) return true;
return false;
}
public function bottomColliding(collider:Formes):Bool
{
if (collider.top >= this.bottom && collider.top < this.top) return true;
return false;
}
Add this to your Formes and Player Classes:
public var bottom(get, null):Float;
public var right(get, null):Float;
public var left(get, null):Float;
public var top(get, null):Float;
private function get_top():Float
{
return this.y;
}
private function get_bottom():Float
{
return this.y + this.height;
}
private function get_left():Float
{
return this.x;
}
private function get_right():Float
{
return this.x + this.width;
}
Example of use:
for(form in formArray)
{
if(player.topColliding(form))
{
//Do something with form touching player's top
}
}
If it does not work or you need more explaination/help just message me.
I fixed collision for The left side but i’ll work on it some more sorry the the code it was more or less an outline but now I try to make it fully implemented.