PDA

View Full Version : How do I access a Sprite in the FLA library from an AS 3.0 file?


matbury
10-27-2007, 01:47 AM
I'm playing with this tutorial script from Foundation Actionscript 3.0 Making Things Move (It's a great book!) I've already learnt a lot of it for AS 2.0, so I'm mostly concerned with tranfering it to AS 3.0.

Everthing works fine but it's using another class - Ball3D - as the Sprites it moves around the stage.

I've drawn a new Sprite in the Flash FLA and I'd like to use that instead. I've redefined its class to Sprite and made the class identifier 'Ball'.

Class: Ball

Base class: flash.display.Sprite

How do I get this class to use the Ball Sprite instead of the Ball3D Sprite?

package {
import flash.display.Sprite;
import flash.events.Event;

public class Rotate3D extends Sprite {
private var balls:Array;
private var numBalls:uint = 50;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;

public function Rotate3D() {
init();
}

private function init():void {
balls = new Array();
for(var i:uint = 0; i < numBalls; i++){
var ball:Ball3D = new Ball3D(10);
balls.push(ball);
ball.xpos = Math.random() * 200 - 100;
ball.ypos = Math.random() * 200 - 100;
ball.zpos = Math.random() * 200 - 100;
addChild(ball);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(event:Event):void {
var angleX:Number = (mouseY - vpY) * .001;
var angleY:Number = (mouseX - vpX) * .001;
for(var i:uint = 0; i < numBalls; i++){
var ball:Ball3D = balls[i];
rotateX(ball, angleX);
rotateY(ball, angleY);
doPerspective(ball);
}
sortZ();
}

private function rotateX(ball:Ball3D, angleX:Number):void {
var cosX:Number = Math.cos(angleX);
var sinX:Number = Math.sin(angleX);
var y1:Number = ball.ypos * cosX + ball.zpos * sinX;
var z1:Number = ball.zpos * cosX - ball.ypos * sinX;
ball.ypos = y1;
ball.zpos = z1;
}

private function rotateY(ball:Ball3D, angleY:Number):void {
var cosY:Number = Math.cos(angleY);
var sinY:Number = Math.sin(angleY);
var x1:Number = ball.xpos * cosY + ball.zpos * sinY;
var z1:Number = ball.zpos * cosY - ball.xpos * sinY;
ball.xpos = x1;
ball.zpos = z1;
}

private function doPerspective(ball:Ball3D):void {
if(ball.zpos > -fl){
var scale:Number = fl / (fl + ball.zpos);
ball.scaleX = ball.scaleY = scale;
ball.x = vpX + ball.xpos * scale;
ball.y = vpY + ball.ypos * scale;
ball.visible = true;
}else{
ball.visible = false;
}
}

private function sortZ():void {
balls.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
for(var i:uint = 0; i < numBalls; i++) {
var ball:Ball3D = balls[i];
setChildIndex(ball, i);
}
}
}
}

Neobii
10-27-2007, 03:29 AM
Don't quite see where your coming from, but you are creating Ball3D objects here.

var ball:Ball3D = new Ball3D(10);

//maybe this is what you want
var ball:Ball = new Ball(10);

matbury
10-27-2007, 04:16 AM
Ah, sorry! I forgot to include the vital Ball3D class. It stores the x, y and z postion data in it, which is one of the reasons why my earlier attempts to use a different display object were failing.

Here's the Ball3D code:
package {
import flash.display.Sprite;
import flash.display.GradientType;
import flash.geom.Matrix;

public class Ball3D extends Sprite {
private var sprite:Sprite;
public var radius:Number;
private var colorInner:uint;
private var colorOuter:uint;
public var xpos:Number = 0;
public var ypos:Number = 0;
public var zpos:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var vz:Number = 0;
public var mass:Number = 1;

public function Ball3D(radius:Number=40, colorInner:uint = 0xffffff, colorOuter:uint=0x000000) {
this.radius = radius;
this.colorInner = colorInner;
this.colorOuter = colorOuter;
init();
}

public function init():void {
sprite = new Sprite();
var offset:Number = -(radius * 1.3)
var colors:Array = [colorInner, colorOuter];
var alphas:Array = [1, 1];
var ratios:Array = [0, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(radius*2, radius*2, (90 * Math.PI / 180), offset, offset);
sprite.graphics.beginGradientFill(GradientType.RAD IAL, colors, alphas, ratios, matrix);
sprite.graphics.drawCircle(0, 0, radius);
sprite.graphics.endFill();
addChild(sprite);
}
}
}