Hello everyone, This is the first time posting here on the forums for me so Ill introduce myself a bit before explaining the problem.
My name is Kevin Mol, currently studying Communication and Multimedia Design on Avans Breda. I'm 20 years old and in the second year now. This semester we were asked to create a serious game for another study on the uni. Currently in the last phase of coding I've encountered a vital error in our design. Please Note that this is My ( / our) first time using AS3 as source for the game and the first time using Box2D Flash. The reason we went from the physic engine of flash to Box2D is because the huge amount of possibly regarding the world.
The problem, as the title of the thread says I want to create a button that will remove / destroy the current shapes wheel shapes on the stage. However I wish to be able to execute all functions in the world, see it as a "Try again" button.
**
I've tried to upload an .zip file containing the class and AS file however the maximum file size was to big so I've uploaded a screenshot on how the game looks like on my screen
The base of the code has been done through this tutorial:
http://active.tutsplus.com/tutorials/ga ... h-and-as3/**
The current code:
Code:
package nl.kevinmol.cmd
{
// Flash Import
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.BitmapData;
import flash.display.MovieClip
import flash.events.MouseEvent;
// Box2d Import
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2World;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2Body;
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Dynamics.b2Fixture;
import Box2D.Dynamics.b2FixtureDef;
public class main extends Sprite
{
// Variable
public var world:b2World;
public var wheelArray:Array;
public var movieclipArray:Array = new Array();
public var stepTimer:Timer;
public var scaleFactor:Number = 20;//pixels per meter
public var mToPx:Number = 20;
public var pxToM:Number = 1 / mToPx;
public var mousePVec:b2Vec2 = new b2Vec2();
public var bal:balMC = new balMC();
public var wissen:wissenMC = new wissenMC();
public var wheelBody:b2Body
// Drag function
public function main():void
{
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
stage.addEventListener(MouseEvent.CLICK, createWheel);
bal.addEventListener(MouseEvent.CLICK, createWheel_gr);
wissen.addEventListener(MouseEvent.CLICK, again);
getStarted();
}
// Code gebied voor maken van objecten
private function getStarted():void
{
var gravity:b2Vec2 = new b2Vec2(0,10);
world = new b2World(gravity,true);
wheelArray = new Array();
createBoundaries();
stepTimer = new Timer(0.025 * 1000);
stepTimer.addEventListener(TimerEvent.TIMER, onTick);
graphics.lineStyle(3, 0xff0000);
stepTimer.start();
}
private function onTick(a_event:TimerEvent):void
{
graphics.clear();
graphics.lineStyle(3, 0xff0000);
world.Step(0.025, 10, 10);
for each (var wheelBody:b2Body in wheelArray)
{
graphics.drawCircle(
wheelBody.GetPosition().x * 20,
wheelBody.GetPosition().y * 20,
(wheelBody.GetFixtureList().GetShape() as b2CircleShape).GetRadius() * 20
);
}
}
// Stil staan de objecten
private function createWheel(e:MouseEvent):void
{
var wheelBodyDef:b2BodyDef = new b2BodyDef();
//wheelBodyDef.type = b2Body.b2_dynamicBody;
wheelBodyDef.position.Set(mouseX/mToPx, mouseY/mToPx);
var wheelBody:b2Body = world.CreateBody(wheelBodyDef);
var circleShape:b2CircleShape = new b2CircleShape(0.7);
var wheelFixtureDef:b2FixtureDef = new b2FixtureDef();
wheelFixtureDef.shape = circleShape;
var wheelFixture:b2Fixture = wheelBody.CreateFixture(wheelFixtureDef);
var startingVelocity:b2Vec2 = new b2Vec2(2, 2);
wheelBody.SetLinearVelocity(startingVelocity);
wheelArray.push(wheelBody);
}
// Bal met zwaartekracht aan;
private function createWheel_gr(e:MouseEvent):void
{
var wheelBodyDef:b2BodyDef = new b2BodyDef();
wheelBodyDef.type = b2Body.b2_dynamicBody;
wheelBodyDef.position.Set(75 / scaleFactor, 95 / scaleFactor);
var wheelBody:b2Body = world.CreateBody(wheelBodyDef);
var circleShape:b2CircleShape = new b2CircleShape(0.5);
var wheelFixtureDef:b2FixtureDef = new b2FixtureDef();
wheelFixtureDef.shape = circleShape;
wheelFixtureDef.restitution = (0.5 * 0.5) + 0.5;
wheelFixtureDef.friction = (1 * 1.0);
wheelFixtureDef.density = 1 * 20;
var wheelFixture:b2Fixture = wheelBody.CreateFixture(wheelFixtureDef);
var wheelShape:b2CircleShape = wheelFixture.GetShape() as b2CircleShape;
var radius:Number = wheelShape.GetRadius();
var startingVelocity:b2Vec2 = new b2Vec2(20,20);
//wheelBody.SetLinearVelocity(startingVelocity);
wheelArray.push(wheelBody);
var movieClip:CirkelMC = new CirkelMC();
addChild(movieClip);
movieClip.x = 50;
movieClip.y = 50;
movieClip.width = 0.5 * scaleFactor * 2;
movieClip.height = 0.5 * scaleFactor * 2;
movieclipArray.push(new Array(wheelBody, movieClip));
}
// Game Play area
private function createBoundaries():void
{
var groundBodyDef:b2BodyDef = new b2BodyDef();
groundBodyDef.position.Set(0, stage.stageHeight / scaleFactor);
var groundBody:b2Body = world.CreateBody(groundBodyDef);
var groundShape:b2PolygonShape = new b2PolygonShape();
groundShape.SetAsBox(stage.stageWidth / scaleFactor, 1 / scaleFactor);
var groundFixtureDef:b2FixtureDef = new b2FixtureDef();
groundFixtureDef.shape = groundShape;
var groundFixture:b2Fixture = groundBody.CreateFixture(groundFixtureDef);
var rightWallBodyDef:b2BodyDef = new b2BodyDef();
rightWallBodyDef.position.Set(stage.stageWidth / scaleFactor, 0);
var rightWallBody:b2Body = world.CreateBody(rightWallBodyDef);
var rightWallShape:b2PolygonShape = new b2PolygonShape();
rightWallShape.SetAsBox(1 / scaleFactor, stage.stageHeight / scaleFactor);
var rightWallFixtureDef:b2FixtureDef = new b2FixtureDef();
rightWallFixtureDef.shape = rightWallShape;
var rightWallFixture:b2Fixture = rightWallBody.CreateFixture(rightWallFixtureDef);
var leftWallBodyDef:b2BodyDef = new b2BodyDef();
leftWallBodyDef.position.Set(0, 0);
var leftWallBody:b2Body = world.CreateBody(leftWallBodyDef);
var leftWallShape:b2PolygonShape = new b2PolygonShape();
leftWallShape.SetAsBox(1 / scaleFactor, stage.stageHeight / scaleFactor);
var leftWallFixtureDef:b2FixtureDef = new b2FixtureDef();
leftWallFixtureDef.shape = leftWallShape;
var leftWallFixture:b2Fixture = leftWallBody.CreateFixture(leftWallFixtureDef);
var ceilingBodyDef:b2BodyDef = new b2BodyDef();
ceilingBodyDef.position.Set(0, 0);
var ceilingBody:b2Body = world.CreateBody(ceilingBodyDef);
var ceilingShape:b2PolygonShape = new b2PolygonShape();
ceilingShape.SetAsBox(stage.stageWidth / scaleFactor, 1 / scaleFactor);
var ceilingFixtureDef:b2FixtureDef = new b2FixtureDef();
ceilingFixtureDef.shape = ceilingShape;
var ceilingFixture:b2Fixture = ceilingBody.CreateFixture(ceilingFixtureDef);
}
public function onEnterFrameHandler(e:Event):void
{
for(var i:int = 0; i < movieclipArray.length; i++)
{
var movieClip:CirkelMC = movieclipArray[i][1];
var wheelBody:b2Body = movieclipArray[i][0];
movieClip.x = wheelBody.GetPosition().x * scaleFactor;
movieClip.y = wheelBody.GetPosition().y * scaleFactor;
//world.DestroyBody(wheelBody);
}
;
addChild(bal);
addChild(wissen);
wissen.x = 200;
}
public function again(e:MouseEvent):void
{
world.DestroyBody(wheelBody);
}
}
}
Currently I'm getting the following output result / error:
Code:
TypeError: Error #1009: Kan geen eigenschap of methode benaderen via een verwijzing naar een object dat null is.
at Box2D.Dynamics::b2World/DestroyBody()
at nl.kevinmol.cmd::main/again()
I've looked through countless website's however i can not find a source file or example on how to create a "destroy world" button.
Any help will be greatly appreciated.
Kevin Mol
***
My next step will be adding an Hit test to the MC bound to the ball that falls to another MC to go to the next level, I have no idea if the destroy world button code will be usefull as a base for removing everything from the screen
***