In a game I am helping with, we are spawning physics objects that we want to have a limited lifespan. Here is what we have done so far. The first part is in the main code of the game and spawns the objects [ice cubes].
// Start ShotPower counter when mouse is pressed.
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void
{
ChargingShot = true;
//trace("ShotPower");
}
// Spawn new physics cubes when the mouse is released.
stage.addEventListener(MouseEvent.CLICK, mouseCLICKHandler);
function mouseCLICKHandler(event:MouseEvent):void
{
ChargingShot = false;
//trace ("Now trying to shoot.");
bodyDef = new b2BodyDef();
// setting the properties of the ice cube
{
boxDef = new b2BoxDef();
// size of ice cube
boxDef.extents.Set((Math.round(Math.random() * 2 + 13)), (Math.round(Math.random() * 2 + 13)));
// density obviously
boxDef.density = 4.0;
// friction [how easily it slides]
boxDef.friction = 0.013;
// elasticity [how much it bounces]
boxDef.restitution = 0.01;
bodyDef.AddShape(boxDef);
bodyDef.userData = new IceCube();
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 2;
}
// where the ice cubes spawn
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next)
{
if (bb.m_userData is PlayerMc)
{
if (PlayerDirection == 1)
{
bodyDef.position.x = (bb.m_userData.x + 25);
bodyDef.position.y = bb.m_userData.y;
trace("Spawn right");
// applying force to the spawning ice cubes
var ShootingRight:b2Vec2 = new b2Vec2(10*ShotPower, 0);
bodyDef.linearVelocity = ShootingRight;
}
if (PlayerDirection == 2)
{
bodyDef.position.x = (bb.m_userData.x - 25);
bodyDef.position.y = bb.m_userData.y;
trace("Spawn left");
// applying force to the spawning ice cubes
var ShootingLeft:b2Vec2 = new b2Vec2(-10*ShotPower, 0);
bodyDef.linearVelocity = ShootingLeft;
}
//trace(PlayerDirection);
m_world.CreateBody(bodyDef);
addChild(bodyDef.userData);
trace("Your ice cube is created.");
DestroyBody(bodyDef.userData);
trace("Your ice cube is destroyed.");
}
}
//var playerMc:PlayerMc = new PlayerMc();
//addChild(playerMc);
}
And here is the Ice Cube code.
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.utils.*;
public class IceCube extends MovieClip
{
var lifeSpan:Number = 10;
var IceCubetimer:Timer = new Timer(1000, 15);
public function IceCube()
{
trace ("making an ice cube, with a lifeSpan of " + lifeSpan + ".");
//setInterval(IceCubetimerlistener, 1000);
//IceCubetimer = new Timer (1000, 15);
IceCubetimer.start();
// Should this be somewhere else? Ask Jacob
IceCubetimer.addEventListener(TimerEvent.TIMER, IceCubetimerlistener)
}
function IceCubetimerlistener (e:TimerEvent): void
{
trace ("The new lifeSpan is " + lifeSpan);
lifeSpan--;
if (lifeSpan <= 0)
{
trace ("Ice cube has melted.");
}
}
}
}
But, I cannot seem to call the DestroyBody function from either place, and if I have to call it from the main script, how do I know talk to a specific ice cube. The naming system in Box2D is still confusing me a bit I admit.
Thank you for any help.
Garett Kutcher
// Start ShotPower counter when mouse is pressed.
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void
{
ChargingShot = true;
//trace("ShotPower");
}
// Spawn new physics cubes when the mouse is released.
stage.addEventListener(MouseEvent.CLICK, mouseCLICKHandler);
function mouseCLICKHandler(event:MouseEvent):void
{
ChargingShot = false;
//trace ("Now trying to shoot.");
bodyDef = new b2BodyDef();
// setting the properties of the ice cube
{
boxDef = new b2BoxDef();
// size of ice cube
boxDef.extents.Set((Math.round(Math.random() * 2 + 13)), (Math.round(Math.random() * 2 + 13)));
// density obviously
boxDef.density = 4.0;
// friction [how easily it slides]
boxDef.friction = 0.013;
// elasticity [how much it bounces]
boxDef.restitution = 0.01;
bodyDef.AddShape(boxDef);
bodyDef.userData = new IceCube();
bodyDef.userData.width = boxDef.extents.x * 2;
bodyDef.userData.height = boxDef.extents.y * 2;
}
// where the ice cubes spawn
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next)
{
if (bb.m_userData is PlayerMc)
{
if (PlayerDirection == 1)
{
bodyDef.position.x = (bb.m_userData.x + 25);
bodyDef.position.y = bb.m_userData.y;
trace("Spawn right");
// applying force to the spawning ice cubes
var ShootingRight:b2Vec2 = new b2Vec2(10*ShotPower, 0);
bodyDef.linearVelocity = ShootingRight;
}
if (PlayerDirection == 2)
{
bodyDef.position.x = (bb.m_userData.x - 25);
bodyDef.position.y = bb.m_userData.y;
trace("Spawn left");
// applying force to the spawning ice cubes
var ShootingLeft:b2Vec2 = new b2Vec2(-10*ShotPower, 0);
bodyDef.linearVelocity = ShootingLeft;
}
//trace(PlayerDirection);
m_world.CreateBody(bodyDef);
addChild(bodyDef.userData);
trace("Your ice cube is created.");
DestroyBody(bodyDef.userData);
trace("Your ice cube is destroyed.");
}
}
//var playerMc:PlayerMc = new PlayerMc();
//addChild(playerMc);
}
And here is the Ice Cube code.
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.utils.*;
public class IceCube extends MovieClip
{
var lifeSpan:Number = 10;
var IceCubetimer:Timer = new Timer(1000, 15);
public function IceCube()
{
trace ("making an ice cube, with a lifeSpan of " + lifeSpan + ".");
//setInterval(IceCubetimerlistener, 1000);
//IceCubetimer = new Timer (1000, 15);
IceCubetimer.start();
// Should this be somewhere else? Ask Jacob
IceCubetimer.addEventListener(TimerEvent.TIMER, IceCubetimerlistener)
}
function IceCubetimerlistener (e:TimerEvent): void
{
trace ("The new lifeSpan is " + lifeSpan);
lifeSpan--;
if (lifeSpan <= 0)
{
trace ("Ice cube has melted.");
}
}
}
}
But, I cannot seem to call the DestroyBody function from either place, and if I have to call it from the main script, how do I know talk to a specific ice cube. The naming system in Box2D is still confusing me a bit I admit.
Thank you for any help.
Garett Kutcher