firdosh
10-17-2006, 01:41 AM
I am trying to create a carousel effect with AS3. It all works good except that i get my swapdepths to work.
I have a CarouselItem class
package
{
import flash.display.Sprite;
public class CarouselItem extends Sprite
{
private var _angle:Number;
public function CarouselItem(){
super();
this.graphics.lineStyle(0,0xff0000,1);
this.graphics.beginFill(0x000099,1);
this.graphics.drawCircle(0,0,50);
this.graphics.endFill();
}
public function get angle():Number{
return _angle;
}
public function set angle(val:Number):void{
_angle=val;
}
}
}
And here is my main class
package {
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.Event;
public class Carousel extends Sprite
{
private var numOfObj:uint=10;
private var radius:Point;
private var center:Point;
private var speed:Number=0.01;
public function Carousel()
{
super();
radius=new Point(200,75);
center=new Point(stage.stageWidth/2, stage.stageHeight/2);
for(var i:int=0;i<numOfObj;i++){
var t:CarouselItem=new CarouselItem();
t.angle=(i*((Math.PI*2)/numOfObj));
t.addEventListener(Event.ENTER_FRAME,onEnter);
this.addChild(t);
}
stage.frameRate=24;
}
private function onEnter(evt:Event):void{
var obj:CarouselItem=CarouselItem(evt.currentTarget);
obj.x=Math.cos(obj.angle) *radius.x +center.x;
obj.y=Math.sin(obj.angle) *radius.y +center.y;
var scale:Number=obj.y /(center.y+radius.y);
obj.scaleX=obj.scaleY=scale*1;
obj.angle=(obj.angle+speed);
var depth:Number=this.getChildIndex(obj);
this.swapChildrenAt(depth, depth+1);
}
}
}
I get the depth of the current object and then swap it with the one with the higher index.
thanks
cheers :)
firdosh
I have a CarouselItem class
package
{
import flash.display.Sprite;
public class CarouselItem extends Sprite
{
private var _angle:Number;
public function CarouselItem(){
super();
this.graphics.lineStyle(0,0xff0000,1);
this.graphics.beginFill(0x000099,1);
this.graphics.drawCircle(0,0,50);
this.graphics.endFill();
}
public function get angle():Number{
return _angle;
}
public function set angle(val:Number):void{
_angle=val;
}
}
}
And here is my main class
package {
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.Event;
public class Carousel extends Sprite
{
private var numOfObj:uint=10;
private var radius:Point;
private var center:Point;
private var speed:Number=0.01;
public function Carousel()
{
super();
radius=new Point(200,75);
center=new Point(stage.stageWidth/2, stage.stageHeight/2);
for(var i:int=0;i<numOfObj;i++){
var t:CarouselItem=new CarouselItem();
t.angle=(i*((Math.PI*2)/numOfObj));
t.addEventListener(Event.ENTER_FRAME,onEnter);
this.addChild(t);
}
stage.frameRate=24;
}
private function onEnter(evt:Event):void{
var obj:CarouselItem=CarouselItem(evt.currentTarget);
obj.x=Math.cos(obj.angle) *radius.x +center.x;
obj.y=Math.sin(obj.angle) *radius.y +center.y;
var scale:Number=obj.y /(center.y+radius.y);
obj.scaleX=obj.scaleY=scale*1;
obj.angle=(obj.angle+speed);
var depth:Number=this.getChildIndex(obj);
this.swapChildrenAt(depth, depth+1);
}
}
}
I get the depth of the current object and then swap it with the one with the higher index.
thanks
cheers :)
firdosh