Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
0
Members

Access Functions of Classes inside other classes.

A topic by j_sevamo created Dec 16, 2017 Views: 305 Replies: 6
Viewing posts 1 to 4

Hi there,
I have a Class called PlayerBehavior, and another class called EnemyBehavior.
Inside PlayerBehavior, I have a method called LowerHealth().

How do I call LowerHealth from within EvemyBehavior?  I want to make a simple code where if the enemy jumps, the player loses some health.

Thanks in advance!!

To do this, you need to grab the player actor inside EnemyBehavior:

Sup.getActor('Player Actor')

then grab the PlayerBehavior attached to that actor:

Sup.getActor('Player Actor').getBehavior(PlayerBehavior)

then call the LowerHealth method:

Sup.getActor('Player Actor').getBehavior(PlayerBehavior).LowerHealth();


[ note: I just broke down each part of the code, normally you would just type the one line:

Sup.getActor('Player Actor').getBehavior(PlayerBehavior).LowerHealth();

in order to get the desired method ]

(+1)

Thank you Spencer, you are a life saver. With this knowledge I'm able to understand much better how Superpowers works.

May I ask you another question if you don't mind?

Inside my PlayerBehavior class I have:

class PlayerBehavior extends Sup.Behavior {
  public playerCol = this.actor.arcadeBody2D;
  speed = 0.3;
  jumpSpeed = 0.45;
  public health = 100;


From inside EnemyBehavior, I want to check colission with just the player and lower his health.

For this I have:

 if(Sup.ArcadePhysics2D.collides(this.actor.arcadeBody2D, Sup.getActor("Player").getBehavior(PlayerBehavior).playerCol))
       {
        Sup.getActor("Player").getBehavior(PlayerBehavior).LowerHealth(1);
       }


However, the screen goes dark when I try this. What am I doing wrong?

Kind regards.

While I'm not quite sure why the screen is going dark, I can offer a different way to go about this.


In your EnemyBehavior, change the collision check to:

if(Sup.ArcadePhysics2D.collides(this.actor.arcadeBody2D, Sup.getActor("Player").arcadeBody2D)
{
        Sup.getActor("Player").getBehavior(PlayerBehavior).LowerHealth(1);
}

You can then remove

public playerCol = this.actor.arcadeBody2D;

from your PlayerBehavior.


In the future, you can usually just call whatever component you need [ arcadeBody, camera, spriteRenderer, etc. ] directly with:

Sup.getActor('Name Of Actor').NameOfComponent


Hope this helps!

(+1)

Thank you once again Spencer. With your help, I was able to finish my quick test with Superpowers. Awesome!
However, I exported the project, and opening the index.html shows a "Could not load plugin list" error. A quick google search showed that it should run if uploaded to a server, so I did. Unfortunately I get the same results.  This is the link.

Any reason why that could be happening? Thanks in advance, you've been of great help and I really appreciate you taking the time to reply to my questions :)

Hmm, I don't know about that one...

Have you tried uploading it here on itch? I've never used the Azure service before so I'm not sure if it's just an issue on their end or not.