AS 3.0 Moving Circle

Here my first pointless little bit of AS 3.0.

Its just an example of implementing the Tween object with a Sprite object.

AS 3.0 Moving Circle.

**EDIT – ive added another quick example based on mulitple circles.

Create a new ActionScript Project in Flex builder 2, and paste the following code.

MovingCircle[as]package
{

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Shape;
import mx.effects.Tween;

public class example extends MovieClip
{

private var _movingCircle:MovingCircle;

public function example()
{
this._movingCircle = new movingCircle();
}

}

private class MovingCircle extends Sprite
{

private var testShape:Shape;

private var _x_tween:Tween;
private var _y_tween:Tween;

private var _targetX_num:Number;
private var _targetY_num:Number;

private var _startX_num:Number;
private var _startY_num:Number;

public function MovingCircle()
{

this.testShape = new Shape();

this.testShape.graphics.beginFill(0xffffff);
this.testShape.graphics.lineStyle(1, 0×000000);
this.testShape.graphics.drawCircle(0, 0, 20);
this.testShape.graphics.endFill();
this.addChild(this.testShape);

this.startXTween();
this.startYTween();
}

private function startXTween():Void
{
this._startX_num = this.x;
this._targetX_num = Math.random() * 200;

delete this._y_tween;

this._x_tween = new Tween(this, this._startX_num, this._targetX_num, (Math.random() * 1000) + 500, 31);
this._x_tween.setTweenHandlers(this.onXTweenUpdate, this.onXTweenEnd);
}

private function startYTween():Void
{
this._startY_num = this.y;
this._targetY_num = Math.random() * 200;

delete this._y_tween;

this._y_tween = new Tween(this, this._startY_num, this._targetY_num, (Math.random() * 1000) + 500, 31);
this._y_tween.setTweenHandlers(this.onYTweenUpdate, this.onYTweenEnd);
}

private function onXTweenUpdate(val:Object):Void
{
this.x = val;
}

private function onXTweenEnd(val:Object):Void
{
this.x = val;

this.startXTween();
}

private function onYTweenUpdate(val:Object):Void
{
this.y = val;
}

private function onYTweenEnd(val:Object):Void
{
this.y = val;

this.startYTween();
}
}

}[/as]

MovingCircles[as]package
{
import flash.display.Sprite;
import flash.display.Shape;
import mx.effects.Tween;
import flash.util.trace;

public class MovingCircles extends Sprite
{
private var _circles_array:Array;
private var _movingCircle2:MovingCircle;

public function MovingCircles()
{
super();

this.initMovingCircles();
}

private function initMovingCircles():Void
{
this._circles_array = new Array();

for(var i:Number = 0; i < 20; i++)
{
this._circles_array.push(new MovingCircle())
this.addChild(this._circles_array[i]);
}
}
}

private class MovingCircle extends Sprite
{
private var _myCircle:Shape;

private var _x_tween:Tween;
private var _y_tween:Tween;

public function MovingCircle()
{
super();

this.initMovieCircle();
}

private function initMovieCircle():Void
{
this._myCircle = new Shape();

this._myCircle.graphics.beginFill(0xffffff);
this._myCircle.graphics.lineStyle(1, 0×000000);
this._myCircle.graphics.drawCircle(0, 0, 10);
this._myCircle.graphics.endFill();
this.addChild(this._myCircle);

this.startXTween();
this.startYTween();
}

private function startXTween():Void
{
delete this._x_tween;

this._x_tween = new Tween(this, this.x, Math.random() * 200, (Math.random() * 1000) + 500, 31);
this._x_tween.setTweenHandlers(this.onXTweenUpdate, this.onXTweenEnd);
}

private function startYTween():Void
{
delete this._y_tween;

this._y_tween = new Tween(this, this.y, Math.random() * 200, (Math.random() * 1000) + 500, 31);
this._y_tween.setTweenHandlers(this.onYTweenUpdate, this.onYTweenEnd);
}

private function onXTweenUpdate(val:Object):Void
{
this.x = val;
}

private function onXTweenEnd(val:Object):Void
{
this.x = val;

this.startXTween();
}

private function onYTweenUpdate(val:Object):Void
{
this.y = val;
}

private function onYTweenEnd(val:Object):Void
{
this.y = val;

this.startYTween();
}
}
}[/as]

18 Responses to “AS 3.0 Moving Circle”

  1. yul says:

    Can you post the swf please ?

  2. Tink says:

    Sure, i’ve amended the original post.

  3. Mr K says:

    Okay … I copied and pasted that into an AS Project file
    Hit Run and nothing happend … infact I got errors

    A bit more detail on how to use this project?

  4. Tink says:

    Hey Mr K

    Take the MovingCircles example.

    You need to open Flex Builder 2.0 and create a new ‘ActionScript Project’ with the name ‘MovingCircles’. The builder will then create the project and a new MovingCircles.as file, that will open automatically. Replace all the code with the code above in the MovingCircles example.

    Then run the project.

  5. [...] n ActionScript 3.0 Misc examples >> 3 sites Encoding PNGs in ActionScript 3 Tinic Uro Moving circle (source code) DisplayListTest (source code) ActionScript 3.0 Sound [...]

  6. mlk says:

    LOOOOOOOOOOOOOOOOL :D :

  7. [...] l Internet varios sitios en donde publican articulos sobre AS3 y flex enlisto algunos: En Tinks Blog publican varios ejemplos de AS3.0 con clases. En flasguru esta un articulo de las nuevas [...]

  8. Drew LeSueur says:

    I got the code to comile after a few changes

    ——————————————————

    package

    {

    import flash.display.Sprite;

    import flash.display.Shape;

    import mx.effects.Tween;

    //import flash.util.trace;

    public class MovingCircles extends Sprite

    {

    private var _circles_array:Array;

    private var _movingCircle2:MovingCircle;

    public function MovingCircles()

    {

    super();

    this.initMovingCircles();

    }

    private function initMovingCircles():void

    {

    this._circles_array = new Array();

    for(var i:Number = 0; i

  9. Drew LeSueur says:

    It didn’t let me post it all but basically, for me, I had to put the second class outside the package and reput the import statements. I also had to remove the delete statements and typcast the val variable to a number.

  10. Drew LeSueur says:

    I also removed the word private from the second class. I used the mxmlc compiler from adobe’s website (I was not using flex compiler).

    Cool example when I got it working! Thank you!

  11. okie_dokie says:

    Dude! why not have the code bigger in size to that of the page instead of having in that non scrollable container?

  12. Tink says:

    Dude! this is a plugin for WordPress that I didn’t write. If you can edit it and send me a scrollable version that would be great!

  13. [...] ) Encodeando PNGs en ActionScript 3 Tinic Uro Encodeando JPEGs en ActionScript 3 Tinic Uro Moviendo Circulo Prueba de Lista de Visualización Moviendo MovieClip en AS3 Mosaico desde un JPG M [...]

  14. [...] lank snap_preview_added=”spa”>Encoding JPEGs in ActionScript 3 Tinic Uro (source code) Moving circle (source code) [...]

  15. [...] ) Encodeando PNGs en ActionScript 3 Tinic Uro Encodeando JPEGs en ActionScript 3 Tinic Uro Moviendo Circulo Prueba de Lista de Visualización Moviendo MovieClip en AS3 Mosaico desde un JPG [...]

Leave a Reply