Question
AddChild problem
I'm trying to modify the Velocity3D.as code from "Foundation
ActionScript 3.0 Animation" so that I get 2 balls appearing that
two different people can control with different keys. This is AS3
of course. I'm new to AS3 so I think I'm making a simple mistake
with addChild or something else that will be obvious to experienced
programmers.
I created a second ball sprite which I called Ballb.as. this second ball is yellow. The first one is red.
Problem 1: I can't get both balls to appear on screen. Which ever addChild is put last that color appears. However, the controls are always the controls for the original red ball. I'm really confused.
Problem 2: I want to use the "WASD" keys to control the second ball. But I can't figure out the correct syntax for using keyCode numbers for those letters. I've used "F" keys in the code as place holders. But as noted above they don't work even when a yellow ball is created.
Here's the modified Velocity3D.as code...
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Velocity3D extends Sprite
{
private var ball:Ball;
private var xpos:Number = 150;
private var ypos:Number = 0;
private var zpos:Number = 150;
private var vx:Number = 0;
private var vy:Number = 0;
private var vz:Number = 0;
private var friction:Number = .98;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var ballb:Ballb;
private var xposb:Number = 130;
private var yposb:Number = 0;
private var zposb:Number = 150;
private var vxb:Number = 0;
private var vyb:Number = 0;
private var vzb:Number = 0;
public function Velocity3D()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
ball = new Ball();
ballb = new Ballb();
addChild(ball);
addChild(ballb);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
private function onEnterFrame(event:Event):void
{
xpos += vx;
ypos += vy;
zpos += vz;
vx *= friction;
vy *= friction;
vz *= friction;
xposb += vx;
yposb += vy;
zposb += vz;
vxb *= friction;
vyb *= friction;
vzb *= friction;
if(zpos > -fl)
{
var scale:Number = fl / (fl + zpos);
ball.scaleX = ball.scaleY = scale;
ball.x = vpX + xpos * scale;
ball.y = vpY + ypos * scale;
ball.visible = true;
}
else
{
ball.visible = false;
}
if(zposb > -fl)
{
var scaleb:Number = fl / (fl + zposb);
ballb.scaleX = ballb.scaleY = scale;
ballb.x = vpX + xpos * scale;
ballb.y = vpY + ypos * scale;
ballb.visible = true;
}
else
{
ball.visible = false;
}
}
private function onKeyDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.NUMPAD_8 :
vy -= 1;
break;
case Keyboard.NUMPAD_2 :
vy += 1;
break;
case Keyboard.NUMPAD_4 :
vx -= 1;
break;
case Keyboard.NUMPAD_6 :
vx += 1;
break;
case Keyboard.UP :
vz += 1;
break;
case Keyboard.DOWN :
vz -= 1;
break;
case Keyboard.F1: //82
vyb -= 1;
break;
case Keyboard.F2 : //67
vyb += 1;
break;
case Keyboard.F3 : //68
vxb -= 1;
break;
case Keyboard.F4 : //70
vxb += 1;
break;
case Keyboard.F5 : //65
vzb += 1;
break;
case Keyboard.F6 : //90
vzb -= 1;
break;
default :
break;
}
}
}
}
Here's Ball.as...
package {
import flash.display.Sprite;
public class Ball extends Sprite {
public var radius:Number;
private var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public function Ball(radius:Number=40, color:uint=0xff0000) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
Here's Ballb.as...
package {
import flash.display.Sprite;
public class Ballb extends Sprite {
public var radius:Number;
private var color:uint;
public var vxb:Number = 0;
public var vyb:Number = 0;
public var mass:Number = 1;
public function Ballb(radius:Number=40, color:uint=0xffff00) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
I created a second ball sprite which I called Ballb.as. this second ball is yellow. The first one is red.
Problem 1: I can't get both balls to appear on screen. Which ever addChild is put last that color appears. However, the controls are always the controls for the original red ball. I'm really confused.
Problem 2: I want to use the "WASD" keys to control the second ball. But I can't figure out the correct syntax for using keyCode numbers for those letters. I've used "F" keys in the code as place holders. But as noted above they don't work even when a yellow ball is created.
Here's the modified Velocity3D.as code...
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Velocity3D extends Sprite
{
private var ball:Ball;
private var xpos:Number = 150;
private var ypos:Number = 0;
private var zpos:Number = 150;
private var vx:Number = 0;
private var vy:Number = 0;
private var vz:Number = 0;
private var friction:Number = .98;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var ballb:Ballb;
private var xposb:Number = 130;
private var yposb:Number = 0;
private var zposb:Number = 150;
private var vxb:Number = 0;
private var vyb:Number = 0;
private var vzb:Number = 0;
public function Velocity3D()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
ball = new Ball();
ballb = new Ballb();
addChild(ball);
addChild(ballb);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
private function onEnterFrame(event:Event):void
{
xpos += vx;
ypos += vy;
zpos += vz;
vx *= friction;
vy *= friction;
vz *= friction;
xposb += vx;
yposb += vy;
zposb += vz;
vxb *= friction;
vyb *= friction;
vzb *= friction;
if(zpos > -fl)
{
var scale:Number = fl / (fl + zpos);
ball.scaleX = ball.scaleY = scale;
ball.x = vpX + xpos * scale;
ball.y = vpY + ypos * scale;
ball.visible = true;
}
else
{
ball.visible = false;
}
if(zposb > -fl)
{
var scaleb:Number = fl / (fl + zposb);
ballb.scaleX = ballb.scaleY = scale;
ballb.x = vpX + xpos * scale;
ballb.y = vpY + ypos * scale;
ballb.visible = true;
}
else
{
ball.visible = false;
}
}
private function onKeyDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.NUMPAD_8 :
vy -= 1;
break;
case Keyboard.NUMPAD_2 :
vy += 1;
break;
case Keyboard.NUMPAD_4 :
vx -= 1;
break;
case Keyboard.NUMPAD_6 :
vx += 1;
break;
case Keyboard.UP :
vz += 1;
break;
case Keyboard.DOWN :
vz -= 1;
break;
case Keyboard.F1: //82
vyb -= 1;
break;
case Keyboard.F2 : //67
vyb += 1;
break;
case Keyboard.F3 : //68
vxb -= 1;
break;
case Keyboard.F4 : //70
vxb += 1;
break;
case Keyboard.F5 : //65
vzb += 1;
break;
case Keyboard.F6 : //90
vzb -= 1;
break;
default :
break;
}
}
}
}
Here's Ball.as...
package {
import flash.display.Sprite;
public class Ball extends Sprite {
public var radius:Number;
private var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public function Ball(radius:Number=40, color:uint=0xff0000) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
Here's Ballb.as...
package {
import flash.display.Sprite;
public class Ballb extends Sprite {
public var radius:Number;
private var color:uint;
public var vxb:Number = 0;
public var vyb:Number = 0;
public var mass:Number = 1;
public function Ballb(radius:Number=40, color:uint=0xffff00) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}