Yahooooooo! (Yes, I might be a redneck). With the help flex coders (i.e.
"y'all"). I was able to port a Flash ActionScript Project to a Flex
ActionScript project. (Well, Joey Lott's ActionScript video class on
Lynda.com helped a lot too. I highly recomend lynda.com for beginners -
like me). The video mentioned that ENTER_FRAME does operate in Flex at a
default of 12fps. (Where do you change that?). So I did not have to resort
to Timer.
All of this is an effort to work through the book "Object Oriented
ActionScript 3.0" in Flex instead of Flash. This is one of the first
examples in the book. (Only the first few examples deal with animation,
but it was bothering me that I had to download and work with the Flash ide
simply because I wanted to learn advanced actionscript). I *highly*
recomend the book. I actualy understand 4 patterns now! Now, I just have to
implement them in flex.
The book uses symbol's as MovieClip but I'm using my BasicShapes (extending
Sprite). Yes, its probably stupidly simple but it has taken me a long time
to figure it out. So I'm posting it in case somebody else needs something
like this.
Here is the main actionscript class:
-----------------------------
package{
import flash.display.MovieClip;
import flash.display.Sprite;
import com.bueche.flex.graphics.BasicShapes;
import com.bueche.flex.graphics.Mover;
public class MoveShapes extends Sprite{
public function MoveShapes (){
var testItem:BasicShapes = new BasicShapes();
var testItemMover:Mover = new Mover(testItem, 2, 3);
addChild(testItem);
testItemMover.startMoving();
}
}//end of Class
}// end of Package
---------------------------------------------end of main class
Here is the BasicShapes class:
-----------------------------------------
package com.bueche.flex.graphics
{
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.JointStyle;
import flash.display.LineScaleMode;
import flash.display.Shape;
import flash.display.Sprite;
public class BasicShapes extends Sprite {
private var size:uint = 80;
private var bgColor:uint = 0xFFCC00;
private var borderColor:uint = 0x666666;
private var borderSize:uint = 0;
private var cornerRadius:uint = 9;
private var gutter:uint = 5;
public function BasicShapes() {
doDrawCircle();
doDrawRoundRect();
doDrawRect();
refreshLayout();
}
private function refreshLayout():void {
var ln:uint = numChildren;
var child:DisplayObject;
var lastChild:DisplayObject = getChildAt(0);
lastChild.x = gutter;
lastChild.y = gutter;
for (var i:uint = 1; i < ln; i++) {
child = getChildAt(i);
child.x = gutter + lastChild.x + lastChild.width;
child.y = gutter;
lastChild = child;
}
}
private function doDrawCircle():void {
var child:Shape = new Shape();
var halfSize:uint = Math.round(size/2);
child.graphics.beginFill(bgColor);
child.graphics.lineStyle(borderSize, borderColor);
child.graphics.drawCircle(halfSize, halfSize, halfSize);
child.graphics.endFill();
addChild(child);
}
private function doDrawRoundRect():void {
var child:Shape = new Shape();
child.graphics.beginFill(bgColor);
child.graphics.lineStyle(borderSize, borderColor);
child.graphics.drawRoundRect(0, 0, size, size, cornerRadius);
child.graphics.endFill();
addChild(child);
}
private function doDrawRect():void {
var child:Shape = new Shape();
child.graphics.beginFill(bgColor);
child.graphics.lineStyle(borderSize, borderColor);
child.graphics.drawRect(0, 0, size, size);
child.graphics.endFill();
addChild(child);
}
}//end class
}//end of package
-------------------------end of BasicShapes class
Here is the mover class:
package com.bueche.flex.graphics
{
import flash.display.MovieClip;
import flash.events.Event;
import com.bueche.flex.graphics.BasicShapes;
public class Mover extends MovieClip {
public var targetShape:BasicShapes;
public var xVel:Number;
public var yVel:Number;
public function Mover(targetShape:BasicShapes, xVel:Number,
yVel:Number) {
this.targetShape = targetShape;
this.xVel = xVel;
this.yVel = yVel;
}
public function updatePosition(evtObj:Event):void {
this.targetShape.x += this.xVel;
this.targetShape.y += this.yVel;
}
public function startMoving():void{
this.targetShape.addEventListener(Event.ENTER_FRAME,
this.updatePosition);
}
public function stopMoving():void{
this.targetShape.removeEventListener(Event.ENTER_FRAME,
this.updatePosition);
}
}
}