Skip to main content
Known Participant
March 28, 2014
Answered

Dynamically storing properties depending on position

  • March 28, 2014
  • 1 reply
  • 448 views

Okay i am creating a tile based game, everytime i move the character i want to store his walking direction for example dirx = 1 , diry = 0. The idea being that when my enemies reach a tile where the character has been it can load the directions and walk the same way. How do i associate the directions with the current tile?

Something like this is my idea but i dont know how to actually program it.

private var giveTileDirections():void

{

          var xdir = this.dirx;

          var ydir = this.diry;

}

Thanks in advance

This topic has been closed for replies.
Correct answer Ned Murphy

How you write the code depends on what class the code is associated with.  "this" will be identifying the class object, which based on your description will be a tile, so if you want to assign the direction of some other object (the character) then you want to target that character, not "this"

When you declare the function, you use the word "function", not "var"

When you declare the variables you should designate what class they are, and they should be declared outside the function if you intend that they be available outside of the class.

So the code might look something like this but only you can figure out how you actually want to program it and what you actually intended for each thing you showed....

public var xdir:int;

public var ydir:int;

private function giveTileDirections():void

{

          xdir = character.dirx;

          ydir = character.diry;

}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
March 28, 2014

How you write the code depends on what class the code is associated with.  "this" will be identifying the class object, which based on your description will be a tile, so if you want to assign the direction of some other object (the character) then you want to target that character, not "this"

When you declare the function, you use the word "function", not "var"

When you declare the variables you should designate what class they are, and they should be declared outside the function if you intend that they be available outside of the class.

So the code might look something like this but only you can figure out how you actually want to program it and what you actually intended for each thing you showed....

public var xdir:int;

public var ydir:int;

private function giveTileDirections():void

{

          xdir = character.dirx;

          ydir = character.diry;

}

Known Participant
March 28, 2014

Wow i was really tired when i wrote this discussion. Thanks for your help i figured it out. How do i remove a discussion like this one?

Ned Murphy
Legend
March 28, 2014

The only way you can remove a discussion is to do so before someone answers it.  It won't hurt to keep it in the event someone else learns something from it.