Crossing between parent and child function

so im not sure if this is a good or bad way to go about what i am doing but it seems to work the best for performance

array control is an array of tiles (children of this class) that make up the screen they all move in the same direction at the same rate i need code to ask the middle tile its type and oriantation to tell this class where or not to change direction.

if (ScreenManager.gS == GameContol.PLAY || ScreenManager.gS == GameContol.START)
{
	for (a in arrayControl)
	{
		a.x += moveSpeedX;
		a.y += moveSpeedY;

		if (a.x == 160 && a.y == 240)
		{
			NewTile.typeReturn();
		}
	}
}

so my understanding is that typeReturn needs to be a static public function which can only take static variables but my understanding of the static keyword is, it will always be the same across the board eg. all my tiles orientations will be the same (change one they all change).

please reposed in a way a dyslexic kid can understand :slight_smile: x

There are many different ways you could design this, but here are a couple

If you the tile type is determined by a type property (say or a String, or better, an enum type), you could do something like this:

enum TileType {
   var A;
   var B;
}

...

if (a.x == 160 && a.y == 240 && a.type == A) {

}

Another alternative, if you use different class types, is to use a common function, which could handle differences between tile types

class Tile extends Sprite {
   
   public function new () {
      
      super ();
      
   }
   
   public function update (dx:Float, dy:Float):Void {
      
      x += dx;
      y += dy;
      
   }
   
}

class SpecialTile extends Tile {
   
   public function new () {
      
      super ();
      
   }
   
   public function update (dx:Float, dy:Float):Void {
      
      super.update (dx, dy);
      
      if (x == 160 && y == 240) {
         
         // do something special
         
      }
      
   }
   
}

You would use new Tile () or new SpecialTile (), depending upon which one you need, and add it to your tiles array

You might also keep your (160, 240) coordinate logic in the parent function, but do something like a reverse () function on the Tile class, so different kinds of tiles can handle the reversal differently. You might also store a speed or direction variable so tiles if tiles ever move in different directions.

If you use a special class for your special type, then different tile types can handle themselves differently. In architecture, it’s sort of a balance of much intelligence you put into each individual object, or how much you rely on a master function to control everything. I sometimes think of it as a work relationship, between a manager and an employee. A manager should be able to ask an employee to “please do this for me”, but should not necessarily be overwhelmed with too many details

I hope this helps! :slight_smile:

hello thank you so much for replying

but im afraid either i dont get it or i miss explained myself

i feel like the first example should help but how do you access TileType for each tile (when in the middle of screen) when they all have random variables/enums. eg. all tiles are straight then there is a bend when it hit the the middle of this screen i turn and go in a different direction.

i can’t work out how to find the childs variables when there is a lots of tiles each with different types and orientations

Let’s say you have a custom Tile class

class Tile {
   
   public function new () {
      
   }
   
}

You could add a type property

class Tile {
   
   public var type:TileType;
   public var x:Float;
   public var y:Float;
   
   public function new () {
      
   }
   
}

enum TileType {
   STRAIGHT;
   RIGHT_CURVE;
}

and you can create them differently, depending on the type:

var tiles = new Array<Tile> ();

var tile = new Tile ();
tile.type = STRAIGHT;
tiles.push (tile);

var tile2 = new Tile ();
tile2.type = RIGHT_CURVE;
tiles.push (tile2);

You can also accept type in the new method to make it more convenient

public function new (type:TileType) {
   
   this.type = type;
   
}

...

var tile = new Tile (STRAIGHT);

then, you can check the tile.type property when you need to, or do a switch:

switch (tile.type) {
   case STRAIGHT: tile.x += 100;
   case RIGHT_CURVE: tile.x += 50; tile.y += 50;
}

thank you your a hero seem so simple now :slight_smile:

1 Like