String Avoider Starling prototype using Starling-Extension-Graphics library and Flash graphic API methods
Talking about String Avoider game, Actionscript 3, Flash, Game development and Starling.
One of the things I was missing the most in Starling framework is I can’t access to AS3 graphic API. I wanted do do a quick port of Stringy game but since it’s mostly based upon graphic API, it seemed impossible… I wasn’t satisfied of my iPhone String Avoider prototype with Adobe Flash Professional CS5.5 so I dropped the project until I found Starling-Extension-Graphics which adds a suite of graphics primitives such as Planes, Fills and Strokes.
With this awesome library I was finally able to make a decent port of the string avoider prototype, have a look:
Main class:
package {
import flash.display.Sprite;
import starling.core.Starling;
[SWF(width="640",height="480",frameRate="60",backgroundColor="#ffffff")]
public class Main extends Sprite {
private var _starling:Starling;
public function Main() {
_starling=new Starling(Game,stage);
_starling.showStats=true;
_starling.start();
}
}
}
Game class:
package {
import flash.geom.Point;
import starling.display.Sprite;
import starling.events.TouchEvent;
import starling.events.Touch;
import starling.events.TouchPhase;
import starling.display.Shape; // <- this belongs to Starling-Extension-Graphics
public class Game extends Sprite {
private var tailLenght:Number=1;
private var tailNodes:Number=300;
// Look, I am creating shapes as if they were sprites
private var bgShape:Shape=new Shape();
private var tailShape:Shape=new Shape();
private var nodes:Vector.<Point>=new Vector.<Point>();
public function Game() {
// same thing as the good old flash graphic class
addChild(bgShape);
bgShape.graphics.beginFill(0x000000);
bgShape.graphics.drawRect(0, 0,640, 480);
bgShape.graphics.endFill();
addChild(tailShape);
for (var i:Number=0; i<tailNodes; i++) {
nodes[i]=new Point(320,240);
}
addEventListener(TouchEvent.TOUCH,touched);
}
private function touched(e:TouchEvent):void {
// checking if we are dragging the mouse
var mouseMoved:Touch=e.getTouch(this,TouchPhase.MOVED);
if (mouseMoved) {
// retrieving mouse location
var localPos:Point=mouseMoved.getLocation(this);
// removing and disposing old shape then creating a new one allows us to save memory
removeChild(tailShape,true);
tailShape=new Shape();
tailShape.graphics.clear();
tailShape.graphics.lineStyle(2,0xffffff);
tailShape.graphics.beginFill(0xffffff);
tailShape.graphics.drawCircle(localPos.x,localPos.y,5);
tailShape.graphics.endFill();
tailShape.graphics.moveTo(localPos.x,localPos.y);
nodes[0]=new Point(localPos.x,localPos.y);
for (var i:Number=1; i<tailNodes-1; i++) {
var nodeAngle:Number=Math.atan2(nodes[i].y-nodes[i-1].y,nodes[i].x-nodes[i-1].x);
nodes[i]=new Point(nodes[i-1].x+tailLenght*Math.cos(nodeAngle),nodes[i-1].y+tailLenght*Math.sin(nodeAngle));
tailShape.graphics.lineTo(nodes[i].x,nodes[i].y);
}
addChild(tailShape);
}
}
}
}
And this is the result, running at 60fps:
Click and drag the mouse to animate the string. It runs at 60fps smoothly, so tomorrow I am porting it to my iPad and will show you both the source code and the result.
Download the source code with all needed libraries
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.