Talking pet (ActionScript tutorial): Difference between revisions

From Whirled Club Wiki
Jump to navigation Jump to search
(Created page with "{{Infobox tutorial |type=ActionScript |difficulty=Intermediate |description=Create a talking pet using AS3. |requirements=20px ActionScript 3....")
 
m (Text replacement - ">" to ">")
 
(2 intermediate revisions by the same user not shown)
Line 12: Line 12:
== A pet that can listen and speak ==
== A pet that can listen and speak ==


Add a [http://www.whirled.com/code/asdocs/com/whirled/ControlEvent.html#CHAT_RECEIVED CHAT_RECEIVED] event listener to call the "handleChat" function every time someone chats in the room.
Add a [http://www.whirled.com/code/asdocs/com/whirled/ControlEvent.html#CHAT_RECEIVED CHAT_RECEIVED] event listener to call the "handleChat" function every time someone chats in the room.


<actionscript>
<actionscript>
_ctrl.addEventListener(ControlEvent.CHAT_RECEIVED, handleChat);
_ctrl.addEventListener(ControlEvent.CHAT_RECEIVED, handleChat);
&lt;/actionscript&gt;
</actionscript>


Use [http://first.whirled.com/code/asdocs/com/whirled/PetControl.html#sendChat%28%29 sendChat] to speak.
Use [http://first.whirled.com/code/asdocs/com/whirled/PetControl.html#sendChat%28%29 sendChat] to speak.


&lt;actionscript&gt;
<actionscript>
_ctrl.sendChat(&quot;hello world&quot;);
_ctrl.sendChat("hello world");
&lt;/actionscript&gt;
</actionscript>


== Main scene ==
== Main scene ==
This code goes under the base code on the main scene.
This code goes under the base code on the main scene.
&lt;actionscript&gt;
<actionscript>
    
    
import flash.events.Event;
import flash.events.Event;
Line 39: Line 39:
function handleChat (event :ControlEvent) :void
function handleChat (event :ControlEvent) :void
{//The words in quotes are the ones you say in whirled to trigger the state change.
{//The words in quotes are the ones you say in whirled to trigger the state change.
     if (event.value == &quot;Play, My Pet&quot;) {
     if (event.value == "Play, My Pet") {
   _ctrl.setState(&quot;playful&quot;);
   _ctrl.setState("playful");
   }
   }
     if (event.value == &quot;Relax, My Pet&quot;) {
     if (event.value == "Relax, My Pet") {
   _ctrl.setState(&quot;content&quot;);
   _ctrl.setState("content");
   }
   }


   //The next if statement makes the pet talk. This is done by sendChat();
   //The next if statement makes the pet talk. This is done by sendChat();
   //Anything inside the parenthesis must be in quotes
   //Anything inside the parenthesis must be in quotes
   if (event.value == &quot;Hello my pet!&quot;) {
   if (event.value == "Hello my pet!") {
   _ctrl.sendChat(&quot;Hello, master!&quot;);
   _ctrl.sendChat("Hello, master!");
   }
   }
  return;
  return;
}
}


&lt;/actionscript&gt;
</actionscript>


[[Category:Code tutorials]]
[[Category:Code tutorials]]

Latest revision as of 06:36, 5 September 2018

ActionScript Tutorial
Create a talking pet using AS3.
Difficulty Level
Intermediate
Requirements
Icon-Programming.png ActionScript 3.0, Whirled SDK
Other Information
Other tutorials: Entity communication (ActionScript tutorial)
Icon-highlighted-pet.png

This code snippet explains how to have your pet listen to and respond to chat events.


A pet that can listen and speak

Add a CHAT_RECEIVED event listener to call the "handleChat" function every time someone chats in the room.

<actionscript> _ctrl.addEventListener(ControlEvent.CHAT_RECEIVED, handleChat); </actionscript>

Use sendChat to speak.

<actionscript> _ctrl.sendChat("hello world"); </actionscript>

Main scene

This code goes under the base code on the main scene. <actionscript>

import flash.events.Event; import com.whirled.EntityControl; import com.whirled.ControlEvent;

_ctrl.addEventListener(ControlEvent.CHAT_RECEIVED, handleChat); //The way pets change states is slightly different then avatars. //The state has to be one of the moods that your pet uses //also note, how the state name is just playful, not the full name playful_idle

function handleChat (event :ControlEvent) :void {//The words in quotes are the ones you say in whirled to trigger the state change.

   if (event.value == "Play, My Pet") {
 _ctrl.setState("playful");
 }
   if (event.value == "Relax, My Pet") {
 _ctrl.setState("content");
 }
 //The next if statement makes the pet talk. This is done by sendChat();
 //Anything inside the parenthesis must be in quotes
  if (event.value == "Hello my pet!") {
 _ctrl.sendChat("Hello, master!");
 }
return;

}

</actionscript>