Array storing coordinates

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;
}}

That is my Formes.hx class

Maybe Have an Array of Forms in your Main.hx ->

var formArray:Array<Formes> = [];

var form:Formes = new Formes(0, 0, 100, 100);
formArray.push(form);

then to get the properties

trace(formArray[0].x);
1 Like

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.

You could use a for loop like:

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;
}

There are also the “hitTest” methods, like “hitTestObject” or “hitTestPoint”

Yes I Know @singmajesty but I need just to make my own class of collision

I’m making my own collision solution too, when it’s efficient/done I could share it with you if you want.

Oh yes. Thanks Thanks

Here’s What I’m using so far:

Add this to your Player Class:

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. :wink:

1 Like

Youpi!!! Thanks. I’ll try immediately. Thanks. :relaxed:

I tried to implement these functions but I am having problems. My code is here: https://www.dropbox.com/s/ajy9m5usfi2ywmi/Collision.rar?dl=0

I fixed collision for The left side but i’ll work on it some more :kissing_smiling_eyes: sorry the the code it was more or less an outline but now I try to make it fully implemented.

This was simply a quick/temporary fix but try it out. It may not be the most optimal but i’ll work on a better solution for collision detection.

https://dl.dropboxusercontent.com/u/196798978/Collision.rar

Thank you. I am trying to also review code to understand and fix bugs in the collision.