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

Referencing own position inside class

Community Beginner ,
Nov 09, 2017 Nov 09, 2017

Copy link to clipboard

Copied

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

Views

573

Translate

Translate

Report

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.

Votes

Translate

Translate
Community Expert ,
Nov 10, 2017 Nov 10, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

This was very helpful, thank you.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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