Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Referencing own position inside class

Community Beginner ,
Nov 09, 2017 Nov 09, 2017

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.

TOPICS
ActionScript
772
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 10, 2017 Nov 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.

Translate
Community Expert ,
Nov 10, 2017 Nov 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 10, 2017 Nov 10, 2017

This was very helpful, thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 11, 2017 Nov 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 11, 2017 Nov 11, 2017
LATEST

The only problem is that with this code all movement can be held down indefinitely. Not the best way to have a jump feature in a game.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 10, 2017 Nov 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 10, 2017 Nov 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines