Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving difficulty updating object properties in basic actionscript program
    primarykey
    data
    text
    <p>I'm still working my way through some pretty basic Actionscript programming (in Flex), and I'm getting a little caught up with events and object properties.</p> <p>I've got 3 files below, one application and two components. The basic goal is very simple - start out with two location canvases and one player canvas. When clicking in one location canvas, the player will update it's x and y coords to that canvas. Clicking in the other canvas will then update the player's x and y coordinates to that canvas. Repeat.</p> <p>I think I'm close, but I can't quite figure out how to get a click within location1 or location2 to send their respective x and y coordinates back to the playerX and playerY values. Any suggestions about this specific issue, or even broader suggestions for accomplishing this more easily, would be great.</p> <p>Here's my primary application file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="components.*"&gt; &lt;mx:Script&gt; &lt;![CDATA[ [Bindable] private var playerX:int = 20; [Bindable] private var playerY:int = 20; private function clickEventHandler(evt:Event):void { playerX = evt.x; playerY = evt.y; } ]]&gt; &lt;/mx:Script&gt; &lt;ns1:location x="139" y="168" id="location1" playerMove="clickEventHandler(event)"/&gt; &lt;ns1:location x="629" y="168" id="location2" playerMove="clickEventHandler(event)"/&gt; &lt;ns1:player x="{playerX}" y="{playerY}"/&gt; &lt;/mx:Application&gt; </code></pre> <p>Here's my "location" component:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="200" height="200" click="clickHandler()"&gt; &lt;mx:Script&gt; &lt;![CDATA[ import flash.events.Events private function clickHandler():void { var moveClick:Event = new Event("playerMove"); moveClick.x = target.x; moveClick.y = target.y; dispatchEvent(moveClick); } ]]&gt; &lt;/mx:Script&gt; &lt;mx:Metadata&gt; [Event(name="playerMove")] &lt;/mx:Metadata&gt; &lt;/mx:Canvas&gt; </code></pre> <p>And here's my player component:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="50" height="100"&gt; &lt;/mx:Canvas&gt; </code></pre> <p>UPDATE:</p> <p>I've been able to simulate the desired effect with the following code:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"&gt; &lt;mx:Script&gt; &lt;![CDATA[ [Bindable] private var playerX:int = 20; [Bindable] private var playerY:int = 20; ]]&gt; &lt;/mx:Script&gt; &lt;mx:Canvas x="139" y="168" id="location1" width="200" height="200" click="playerX = location1.x, playerY = location1.y" backgroundColor="#F60D0D"/&gt; &lt;mx:Canvas x="629" y="168" id="location2" width="200" height="200" click="playerX = location2.x, playerY = location2.y" backgroundColor="#EDE513"/&gt; &lt;mx:Canvas id="player1" x="{playerX}" y="{playerY}" width="100" height="100" backgroundColor="#3458F5"/&gt; &lt;/mx:Application&gt; </code></pre> <p>Now I'm just working on modularizing it to achieve my real end goal, which is making sure I understand how events, objects, and event objects work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload