Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by carlos_moreira

Hello all,

I have created a switch entity, that the player can turn on and off.

I wanted to know how Could I connect that 'Switch' to another object such as a moving platform.

On my moving platform, I have tried using getEntitybyName(theSwith);
and asking if the switch is in the on position , then the moving platform should move.

Yet my game crashes when using that method.

Is there something I may be missing on how to connect entities? I'm using the "Mover" Entity to move a platform up and down. I just want it only to move if the switch is "on."

Any help would be appreciated.

Thank you

1 decade ago by giodif

First, look here:

http://impactjs.com/documentation/weltmeister

near the bottom of the page, Dominic explains how to make connections between entities in weltmeister.

Once you've made your connections take a look at the biolab-entity-pack and take a look at EntityTrigger. This is a good example of how to send messages from one entity to another.

Essentially, you'd do something like this:

Imagine you have three entities: Player, Switch, and Platform

In wetmeister:

Set Switch's 'target.1' property to Platform's name property ( See above link ).

Desired behavior:

1. Player flips Switch
2. Switch causes Platform to do something, ie. to start moving.

Steps in code:

1. Player flips Switch, which turns on the Switch. I'm assuming you're doing this in the check function, which is cool.

2. Switch uses the value stored in its own 'target' property to grab a reference to Platform. ( Switch.target.1 === Platform.name ). Use ig.game.getEntityByName(this.target.1).

3. Now that Switch knows Platform, it can send Platform a message to turn itself on.

4.Platform turns itself on.

Code ( Just example stuff, you'll need to tweak this ):

Player doesn't really need to do anything

Switch:

// when Player touches Switch, it turns itself on

check : function( other ){

   //if Action button is pressed
   //Assuming that the player has to press a button to turn on Switch  
   
   if( ig.input.state( 'action' ) ){

      //if other is Player, turn on switch.
      if( other === Player )

         this.on();
      }
}

// the on function should do three things

on : function(){

   // 1. turn myself on

   this.isOn = true; //or however you are already handling this.

   // 2. get reference to Platform

   var platform = ig.game.getEntityByName( this.target.1 );

   // 3. turn on platform

   platform.on();
}


Platform:

//You'll need one property on Platform

shouldMove : false

//Platform turns itself on

on : function(){

   //Start moving.

   //This could be a simple call to a state machine that changes how
   //your update function works. 

   this.shouldMove = true;
}


Your update function could looks something like this:

update : function(){

   if( this.shouldMove ){
      //Move myself
   }
}

This way, Platform won't move until it's been messaged by Switch.

These functions and properties can be whatever you want. The above code is just to explain the concepts. You'll need to have a reference to Player to use in the check function ( I'm assuming you already know how to handle the checks ).

Thats pretty long-winded but, I hope it help get the idea across.

1 decade ago by carlos_moreira

Wow, I was doing it all wrong, this is somewhat how I did mine.

Except I was trying to call "ig.game.getEntityByName" inside the init method, which does not let you call it there.

1 decade ago by giodif

Yeah, if you want to set things up during entity creation, use the ready method instead of init.

However, I like to scope references to other objects within methods so that Entities aren't carrying references to other Entities all the time.
Page 1 of 1
« first « previous next › last »