Entity communication (ActionScript tutorial): Difference between revisions

From Whirled Club Wiki
Jump to navigation Jump to search
m (Text replacement - "<" to "<")
m (Text replacement - ">" to ">")
Line 19: Line 19:
You can use EntityControl.addEventListener() to listen for [http://whirled.com/code/asdocs/com/whirled/EntityControl.html#event:entityEntered entityEntered], [http://whirled.com/code/asdocs/com/whirled/EntityControl.html#event:entityEntered entityMoved] and [http://whirled.com/code/asdocs/com/whirled/EntityControl.html#event:entityEntered entityLeft] events:
You can use EntityControl.addEventListener() to listen for [http://whirled.com/code/asdocs/com/whirled/EntityControl.html#event:entityEntered entityEntered], [http://whirled.com/code/asdocs/com/whirled/EntityControl.html#event:entityEntered entityMoved] and [http://whirled.com/code/asdocs/com/whirled/EntityControl.html#event:entityEntered entityLeft] events:


<actionscript&gt;
<actionscript>
_ctrl = new PetControl(this);
_ctrl = new PetControl(this);
_ctrl.addEventListener(ControlEvent.ENTITY_MOVED, handleMovement);
_ctrl.addEventListener(ControlEvent.ENTITY_MOVED, handleMovement);
Line 27: Line 27:
     _ctrl.sendChat(&quot;Something's moving around!&quot;);
     _ctrl.sendChat(&quot;Something's moving around!&quot;);
}
}
</actionscript&gt;
</actionscript>


== Requesting Properties ==
== Requesting Properties ==
Line 33: Line 33:
The ControlEvent contains the entity ID of the mover, we can use that to access properties on that entity using EntityControl.getEntityProperty():
The ControlEvent contains the entity ID of the mover, we can use that to access properties on that entity using EntityControl.getEntityProperty():


<actionscript&gt;
<actionscript>
function handleMovement (event :ControlEvent) :void
function handleMovement (event :ControlEvent) :void
{
{
Line 41: Line 41:
     _ctrl.sendChat(&quot;I see you &quot; + _ctrl.getEntityProperty(EntityControl.PROP_NAME, targetId));
     _ctrl.sendChat(&quot;I see you &quot; + _ctrl.getEntityProperty(EntityControl.PROP_NAME, targetId));
}
}
</actionscript&gt;
</actionscript>


Now our pet will announce when it sees an avatar (or another pet) moving in the room. Let's improve this a bit to make the pet follow any movement:
Now our pet will announce when it sees an avatar (or another pet) moving in the room. Let's improve this a bit to make the pet follow any movement:


<actionscript&gt;
<actionscript>
function handleMovement (event :ControlEvent) :void
function handleMovement (event :ControlEvent) :void
{
{
Line 59: Line 59:
     }
     }
}
}
</actionscript&gt;
</actionscript>


== Custom Property Providers ==
== Custom Property Providers ==
Line 65: Line 65:
Let's make a food bowl that our pet can go to when he's hungry. The food bowl will be a separate toy item, and can use a property provider to respond to our pet when he asks for food.
Let's make a food bowl that our pet can go to when he's hungry. The food bowl will be a separate toy item, and can use a property provider to respond to our pet when he asks for food.


<actionscript&gt;
<actionscript>
// In Food.as
// In Food.as


Line 81: Line 81:
     return null;
     return null;
}
}
</actionscript&gt;
</actionscript>


Let's modify our pet to look for food when someone in the room says &quot;go eat&quot;:
Let's modify our pet to look for food when someone in the room says &quot;go eat&quot;:


<actionscript&gt;
<actionscript>
// In Dog.as
// In Dog.as


Line 101: Line 101:


             // If food was returned
             // If food was returned
             if (food &gt; 0) {
             if (food > 0) {
                 _ctrl.sendChat(&quot;*munch munch*&quot;);
                 _ctrl.sendChat(&quot;*munch munch*&quot;);


Line 116: Line 116:
     }
     }
}
}
</actionscript&gt;
</actionscript>


=== Calling Remote Functions ===
=== Calling Remote Functions ===
Line 122: Line 122:
Property providers allows an entity to respond to simple messages passed to it. In this case, the messages are just strings. What if we want to specify parameters in the message? For example, a Knight avatar may send an &quot;attackForDamage&quot; message along with the amount of damage it is trying to inflict. In this hypothetical case, a slayable Dragon pet would listen for &quot;attackForDamage&quot; and respond:
Property providers allows an entity to respond to simple messages passed to it. In this case, the messages are just strings. What if we want to specify parameters in the message? For example, a Knight avatar may send an &quot;attackForDamage&quot; message along with the amount of damage it is trying to inflict. In this hypothetical case, a slayable Dragon pet would listen for &quot;attackForDamage&quot; and respond:


<actionscript&gt;
<actionscript>
// In Dragon.as
// In Dragon.as


Line 138: Line 138:
     return null;
     return null;
}
}
</actionscript&gt;
</actionscript>


<actionscript&gt;
<actionscript>
// In Knight.as
// In Knight.as


Line 151: Line 151:
// Or... have damage be based on your Knight's &quot;level&quot;
// Or... have damage be based on your Knight's &quot;level&quot;
attackForDamage(10 * (_ctrl.getMemory(&quot;level&quot;) as Number));
attackForDamage(10 * (_ctrl.getMemory(&quot;level&quot;) as Number));
</actionscript&gt;
</actionscript>


== Helper Class: RemoteEntity ==
== Helper Class: RemoteEntity ==
Line 157: Line 157:
If your project use entity properties heavily, consider using the convenient [http://whirled.com/code/contrib/asdocs/com/whirled/contrib/RemoteEntity.html RemoteEntity] class instead of direct calls to EntityControl.getEntityProperty():
If your project use entity properties heavily, consider using the convenient [http://whirled.com/code/contrib/asdocs/com/whirled/contrib/RemoteEntity.html RemoteEntity] class instead of direct calls to EntityControl.getEntityProperty():


<actionscript&gt;
<actionscript>
// RemoteEntity example snippet:
// RemoteEntity example snippet:


Line 168: Line 168:
// Same as (_ctrl.getEntityProperty(&quot;attackForDamage&quot;, targetId) as Function)(50);
// Same as (_ctrl.getEntityProperty(&quot;attackForDamage&quot;, targetId) as Function)(50);
remote.call(&quot;attackForDamage&quot;, 50);
remote.call(&quot;attackForDamage&quot;, 50);
</actionscript&gt;
</actionscript>





Revision as of 23:01, 4 September 2018

ActionScript Tutorial
Create an interactive pet using AS3.
Difficulty Level
Intermediate
Requirements
Icon-Programming.png ActionScript 3.0, Whirled SDK
Other Information
Other tutorials: Talking pet (ActionScript tutorial)
Icon-highlighted-pet.png

This tutorial uses AS3 to build a smart pet that is able to detect and interact with other items in the room.

Prerequisites

  1. Setting up your programming environment
  2. This tutorial currently assumes you are familiar with programming and that you can learn by example.

Entity Events

In Whirled, every item (toys, pets, avatars, backdrops...) is called an entity. Every copy of your item in Whirled has its own unique entity ID.

You can use EntityControl.addEventListener() to listen for entityEntered, entityMoved and entityLeft events:

<actionscript> _ctrl = new PetControl(this); _ctrl.addEventListener(ControlEvent.ENTITY_MOVED, handleMovement);

function handleMovement (event :ControlEvent) :void {

   _ctrl.sendChat("Something's moving around!");

} </actionscript>

Requesting Properties

The ControlEvent contains the entity ID of the mover, we can use that to access properties on that entity using EntityControl.getEntityProperty():

<actionscript> function handleMovement (event :ControlEvent) :void {

   var targetId :String = event.name;
   // Use getEntityProperty() to query the target's name
   _ctrl.sendChat("I see you " + _ctrl.getEntityProperty(EntityControl.PROP_NAME, targetId));

} </actionscript>

Now our pet will announce when it sees an avatar (or another pet) moving in the room. Let's improve this a bit to make the pet follow any movement:

<actionscript> function handleMovement (event :ControlEvent) :void {

   var targetId :String = event.name;
   // IMPORTANT: We will receive events from our own movements, so make sure we don't handle them here
   if (targetId != _ctrl.getMyEntityId()) {
       _ctrl.sendChat("I see you " + _ctrl.getEntityProperty(EntityControl.PROP_NAME, targetId));
       
       // Follow it
       var pos :Array = _ctrl.getEntityProperty(EntityControl.PROP_LOCATION_PIXEL, targetId) as Array;
       _ctrl.setPixelLocation(pos[0], pos[1], pos[2], 0);
   }

} </actionscript>

Custom Property Providers

Let's make a food bowl that our pet can go to when he's hungry. The food bowl will be a separate toy item, and can use a property provider to respond to our pet when he asks for food.

<actionscript> // In Food.as

_ctrl = new FurniControl(this); _ctrl.registerPropertyProvider(propertyProvider);

function propertyProvider (key :String) :Object {

   if ("tutorial:takeFood" == key) {
       // Something in the room is requesting food from me, send back a random amount
       return Math.floor(Math.random()*20) + 1;
   }
   // We don't support this key, so return null
   return null;

} </actionscript>

Let's modify our pet to look for food when someone in the room says "go eat":

<actionscript> // In Dog.as

_ctrl.addEventListener(ControlEvent.RECEIVED_CHAT, handleChat);

function handleChat (event :ControlEvent) :void {

   if (event.value == "go eat") {
       // Get all the furniture/toys IDs in the room
       var furnis :Array = _ctrl.getEntityIds(EntityControl.TYPE_FURNI);
       for each (var id :String in furnis) {
           // Try to ask for some food
           var food :Number = _ctrl.getEntityProperty("tutorial:takeFood", id) as Number;
           // If food was returned
           if (food > 0) {
               _ctrl.sendChat("*munch munch*");
               // Walk over to the food bowl
               var pos :Array = _ctrl.getEntityProperty(EntityControl.PROP_LOCATION_PIXEL, id) as Array;
               _ctrl.setPixelLocation(pos[0], pos[1], pos[2], 0);
               return;
           }
       }
       // If we reach this point, we didn't find anything edible
       _ctrl.sendChat("I can't find anything to eat... *whimper*");
   }

} </actionscript>

Calling Remote Functions

Property providers allows an entity to respond to simple messages passed to it. In this case, the messages are just strings. What if we want to specify parameters in the message? For example, a Knight avatar may send an "attackForDamage" message along with the amount of damage it is trying to inflict. In this hypothetical case, a slayable Dragon pet would listen for "attackForDamage" and respond:

<actionscript> // In Dragon.as

_ctrl.registerPropertyProvider(propertyProvider); function propertyProvider (key :String) :Object {

   if (key == "attackForDamage") {
       // Don't actually do anything yet
       // Return a Function object that the Knight can use to damage the dragon
       return function (damage :Number) :void {
           _ctrl.sendChat("Ouch! A Knight hit me for " + x + " damage!");
       }
   }
   return null;

} </actionscript>

<actionscript> // In Knight.as

var dragonId :String = ... // The entity ID of a Dragon var attackForDamage :Function = _ctrl.getEntityProperty("attackForDamage", dragonId) as Function;

// Attack the Dragon for 30 damage attackForDamage(30);

// Or... have damage be based on your Knight's "level" attackForDamage(10 * (_ctrl.getMemory("level") as Number)); </actionscript>

Helper Class: RemoteEntity

If your project use entity properties heavily, consider using the convenient RemoteEntity class instead of direct calls to EntityControl.getEntityProperty():

<actionscript> // RemoteEntity example snippet:

// Set up a remote targeting a certain entity var remote :RemoteEntity = new RemoteEntity(_ctrl, targetId);

// Same as trace("Hello " + _ctrl.getEntityProperty(EntityProperty.PROP_NAME, targetId) as String)) trace("Hello " + remote.getName());

// Same as (_ctrl.getEntityProperty("attackForDamage", targetId) as Function)(50); remote.call("attackForDamage", 50); </actionscript>


Demo

Mr Fusspot gets hungry quickly, but he's happy to play with visitors when he has the energy:

Template:Room

External links