Skip to main content
eliw12510111
Participating Frequently
November 10, 2017
Answered

Referencing own position inside class

  • November 10, 2017
  • 2 replies
  • 881 views

Hi,

This might be kind of hard to explain since I'm so new.

Essentially I'm creating a game where I want the player avatar to have its own class.

I've added event listeners in the main class file that change the string values of variables DirectionY or DirectionX to "Up" "Down" or "Left" "Right", respectively, with the WASD keys.

I'm them using an update function inside of the player class to basically say if DirectionX status is "Right" go right.

The problem is, I have no idea how to reference the position of the movieclip connected to the player class in order to make it move.

The Player is brought on to the stage like so

var player:Player = new Player(); (The library object/visual representation of the player is also called Player, and I've put it's class as Player)

Obviously, I can't just reference the instance name as if it were always on stage.

I really don't want to have all the player avatar code in the main game class.

This topic has been closed for replies.
Correct answer JoãoCésar17023019

I hope this basic game structure can help you: https://goo.gl/rYS4Lc

Notice that I tried to keep things very simple, but even so it's possible to see how powerful inheritance, encapsulation, reasonable distribuction of methods and properties to objects that are more suitable to have them, among other design patterns related applications.

But it's my way of doing things. Of course it's not supposed to the best or the only possible approach.

2 replies

Inspiring
November 10, 2017

In your Player class, you must have something like thisPlayer = new mcPlayer(); which would be an instance from the library... Have a getter in the Player class you can use to get that reference to the actual movie clip.

public function get clipRef():MovieClip

{

     return thisPlayer;

}

And then in your Main class you would get the ref like: thePlayer = player.clipRef;

and you can move, or do whatever, with that.

eliw12510111
Participating Frequently
November 10, 2017

This definitely along the lines of what I'm looking for I'll see if it can work. Yet, do we know this will for sure get the same Movieclip that's been attached to the Class on stage?

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
November 10, 2017

I hope this basic game structure can help you: https://goo.gl/rYS4Lc

Notice that I tried to keep things very simple, but even so it's possible to see how powerful inheritance, encapsulation, reasonable distribuction of methods and properties to objects that are more suitable to have them, among other design patterns related applications.

But it's my way of doing things. Of course it's not supposed to the best or the only possible approach.

eliw12510111
Participating Frequently
November 11, 2017

This was very helpful, thank you.

Inspiring
November 11, 2017

You're welcome. And to answer your question: "Yet, do we know this will for sure get the same Movieclip that's been attached to the Class on stage?"

Yes, of course. It's your code, right. Your main class instantiates a library clip and has a getter that returns a reference to it. Another class gets the reference. Unless you code it, there is no chance for some other, wrong, reference to be returned.