PDA

View Full Version : creating properties for sprite


saveth
07-28-2007, 03:21 AM
Hi,

I'm trying to create two new properties called startX and startY that I can use with a Sprite.

so when I create

var eachBall:Sprite = Sprite(new AnimateBall());

I can set the property startX and startY like so

eachBall.startX = eachBall.x
eachBall.startY = eachBall.y;

this doesn't need to override the x and y it's just merely a datastore. Any ideas how I can accomplish this?

Thanks,
Saveth

saveth
07-28-2007, 04:22 AM
I was able to get a little farther, I created a class that extends sprite and added in two getters and setters for startX and startY. I'm getting another error now though on the two lines of code below.


this.getChildByName("myBall_"+i).x = this.getChildByName("myBall_"+i).startX+Math.sin(angleX)*range;
this.getChildByName("myBall_"+i).y = this.getChildByName("myBall_"+i).startY+Math.cos(angleY)*range;


It says that startX and startY are not propertys of type DisplayObject. Why is it considering what I would think would be Sprite as a display object? The full code is below.

Thanks,
Saveth


package com.crylounge
{
import flash.display.*;
import flash.media.*;
import flash.net.*;
import flash.utils.ByteArray;
import flash.events.*;
import flash.geom.*;
import com.crylounge.ExtendSprite

public class RaveWaveformVisualization extends BaseVisualization
{
private var angleX:Number = 0;
private var angleY:Number = 0;
private var range:Number;
private var xspeed:Number = .1;
private var yspeed:Number = .1;

[Embed(source="/images/AnimateBall.swf")]
[Bindable]
public var AnimateBall:Class;

override protected function updateDisplayList(w:Number, h:Number):void
{
super.updateDisplayList(w, h);
makeCircles();
}

override protected function drawVisualization(e:Event):void
{
if( !this.visible ) return;

SoundMixer.computeSpectrum(spectrum, true, 2);

var vol:Number = 0;

for (var i:uint = 0; i <256; i++) {
var fft:Number = spectrum.readFloat()*12;
//vol is percieved loudness - generated by combining all 512 'lev' values//
vol += fft;
range = fft*50;//vol*2;
if (this.getChildByName("myBall_"+i)) {
this.getChildByName("myBall_"+i).scaleX = this.getChildByName("myBall_"+i).scaleY = fft;
this.getChildByName("myBall_"+i).x = this.getChildByName("myBall_"+i).startX+Math.sin(angleX)*range;
this.getChildByName("myBall_"+i).y = this.getChildByName("myBall_"+i).startY+Math.cos(angleY)*range;
angleX += xspeed;
angleY += yspeed;
}
}
}

public function makeCircles():void {
for (var j:uint = 0; j <256; j++) {
var eachBall:ExtendSprite = ExtendSprite(new AnimateBall());
eachBall.name = "myBall_"+j;
eachBall.x = 380 - 5;
eachBall.y = j*2+30;
eachBall.startX = eachBall.x;
eachBall.startY = eachBall.y;
this.addChild(eachBall);
this.rotation = 45;
this.x = 300;
this.y = - 180;
}
}
}
}

dr_zeus
07-30-2007, 03:33 PM
Sprite is a subclass of DisplayObject. The function getChildByName() will return any DisplayObject, but you need to tell it when you're returning something more specific, like an instance of your AnimateBall class. See fixed code below where I cast the result of getChildByName() as AnimateBall.

this.getChildByName("myBall_"+i).x = AnimateBall(this.getChildByName("myBall_"+i)).startX + Math.sin(angleX)*range;