Thursday, 22 January 2009

Day 4 - Adapted LA 3.0 bookcode for Zeno's paradox for FlashDevelop

Ok, I'm reading through many of the examples in the book, but not testing them all. At the moment it doesn't seem too difficult to adapt the code - and I might not adapt any further examples as for now I've established proof of concept.


To adapt the Zeno's paradox code (explained on pg 127 and see video above) all you need to do is move the instance creating code from the fla to a class file. FD just needs .as files, this book example, like many others, has projects split into class .as files. There is often one .fla file that opens in Flash and might use graphical assets in the Flash Library but then uses standard .as class files that can used in FD without modification.

The class below is the main class in FD, and if you right click it in the project window set it to 'always compile'. Remember class names are the same as file names.

package {
import flash.display.Sprite;
import flash.events.Event;
import Ball;
public class ZenoMain extends Sprite {
private var clip1:Ball;

public function ZenoMain() {
clip1= new Ball(20,50);
clip1.x = clip1.y = 100;
addChild(clip1);
clip1.addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
function onLoop(evt:Event):void {
clip1.x += velFriction(clip1.x, mouseX, 8);
clip1.y += velFriction(clip1.y, mouseY, 8);
}
function velFriction(orig:Number, dest:Number, coeff:Number):Number {
return (dest-orig)/coeff;
}

}

}
}

then you need a code for a class for a ball like

package {
import flash.display.MovieClip;
public class Ball extends MovieClip{
private var xPos:Number;
private var yPos:Number;
public function Ball(xPos:Number,yPos:Number) {
graphics.beginFill( 0xff0000 , 1 );
graphics.drawCircle( 20 , 20 , 20 );
graphics.endFill( );
trace ("Ball falls")
x = xPos;
y = yPos;
}
}
}

No comments:

Post a Comment