Hi everybody.
I´m in a middle of a problem and I can´t understand what is wrong here, so any help would be great.
I´m making some tests and I added by code some Circles and need to drag them, so as I did read here to handle resize events
need to destroy the objects(in my case walls) and recreat all of them and position.
The resize is working fine now but dragging the circles is geeting some troubles.
The circles I´m trying to drag stay far from the mouse cursor.
Here is my code and in the I tried to attached a swf file showing the problem but says "The extension swf is not allowed."
Thanks in advance.
I´m in a middle of a problem and I can´t understand what is wrong here, so any help would be great.
I´m making some tests and I added by code some Circles and need to drag them, so as I did read here to handle resize events
need to destroy the objects(in my case walls) and recreat all of them and position.
The resize is working fine now but dragging the circles is geeting some troubles.
The circles I´m trying to drag stay far from the mouse cursor.
Here is my code and in the I tried to attached a swf file showing the problem but says "The extension swf is not allowed."
Code:
package stagePhysics {
import Box2DAS.*;
import Box2DAS.Collision.*;
import Box2DAS.Collision.Shapes.*;
import Box2DAS.Common.*;
import Box2DAS.Dynamics.*;
import Box2DAS.Dynamics.Contacts.*;
import Box2DAS.Dynamics.Joints.*;
import cmodule.Box2D.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.ui.*;
import flash.utils.*;
import misc.*;
import shapes.Box;
import wck.*;
/**
* A base document class for WCK based Flash SWFs
*/
public class StagePhysics extends Entity {
private var _world;
public override function create():void {
b2Base.initialize();
Input.initialize(stage);
_world = mundo;
createWalls();
addObjects();
stage.addEventListener(Event.RESIZE, onResize);
}
private function createWalls():void{
for(var i:uint=0;i<4;++i){
var _wall:Box;
switch(i){
case 0:
_wall = drawBoxWidth();
_wall.y = stage.stageHeight/2;
break;
case 1:
_wall = drawBoxWidth();
_wall.y = -(stage.stageHeight/2);
break;
case 2:
_wall = drawBoxHeight();
_wall.x = stage.stageWidth/2;
break;
case 3:
_wall = drawBoxHeight();
_wall.x = -(stage.stageWidth/2);
break;
}
_wall.type = "Static";
_wall.name = "wall"+i.toString();
_world.addChild(_wall);
}
}
private function drawBoxWidth():Box{
var b:Box = new Box();
b.graphics.clear();
b.graphics.beginFill(0x00ffff,1);
b.graphics.drawRect(-stage.stageWidth/2,-15,stage.stageWidth,30);
b.graphics.endFill();
return b;
}
private function drawBoxHeight():Box{
var b:Box = new Box();
b.graphics.clear();
b.graphics.beginFill(0x00ffff,1);
b.graphics.drawRect(-15,-stage.stageHeight/2,30,stage.stageHeight);
b.graphics.endFill();
return b;
}
private function onResize(e:Event):void{
for(var i:uint=0;i<4;++i){
var _wall = _world.getChildByName("wall"+i) as Box;
//_wall.destroyBody(); // it throws an error so I comented
_wall.destroy();
_world.removeChild(_wall);
_wall = null;
}
createWalls();
}
private function addObjects():void{
for(var i:uint=0;i<10;++i){
var _circle = new Circle0(); // a object in my library with linkage shapes.Circle
_circle.name = String("circle"+i);
_circle.id = i;
_circle.restitution = 0.5;
_circle.bullet = true;
_circle.buttonMode = true;
_circle.addEventListener(MouseEvent.CLICK, mClick);
_world.addChild(_circle);
}
}
private function mClick(e:MouseEvent):void{
trace(e.currentTarget.id)
}
}
}
import Box2DAS.*;
import Box2DAS.Collision.*;
import Box2DAS.Collision.Shapes.*;
import Box2DAS.Common.*;
import Box2DAS.Dynamics.*;
import Box2DAS.Dynamics.Contacts.*;
import Box2DAS.Dynamics.Joints.*;
import cmodule.Box2D.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.ui.*;
import flash.utils.*;
import misc.*;
import shapes.Box;
import wck.*;
/**
* A base document class for WCK based Flash SWFs
*/
public class StagePhysics extends Entity {
private var _world;
public override function create():void {
b2Base.initialize();
Input.initialize(stage);
_world = mundo;
createWalls();
addObjects();
stage.addEventListener(Event.RESIZE, onResize);
}
private function createWalls():void{
for(var i:uint=0;i<4;++i){
var _wall:Box;
switch(i){
case 0:
_wall = drawBoxWidth();
_wall.y = stage.stageHeight/2;
break;
case 1:
_wall = drawBoxWidth();
_wall.y = -(stage.stageHeight/2);
break;
case 2:
_wall = drawBoxHeight();
_wall.x = stage.stageWidth/2;
break;
case 3:
_wall = drawBoxHeight();
_wall.x = -(stage.stageWidth/2);
break;
}
_wall.type = "Static";
_wall.name = "wall"+i.toString();
_world.addChild(_wall);
}
}
private function drawBoxWidth():Box{
var b:Box = new Box();
b.graphics.clear();
b.graphics.beginFill(0x00ffff,1);
b.graphics.drawRect(-stage.stageWidth/2,-15,stage.stageWidth,30);
b.graphics.endFill();
return b;
}
private function drawBoxHeight():Box{
var b:Box = new Box();
b.graphics.clear();
b.graphics.beginFill(0x00ffff,1);
b.graphics.drawRect(-15,-stage.stageHeight/2,30,stage.stageHeight);
b.graphics.endFill();
return b;
}
private function onResize(e:Event):void{
for(var i:uint=0;i<4;++i){
var _wall = _world.getChildByName("wall"+i) as Box;
//_wall.destroyBody(); // it throws an error so I comented
_wall.destroy();
_world.removeChild(_wall);
_wall = null;
}
createWalls();
}
private function addObjects():void{
for(var i:uint=0;i<10;++i){
var _circle = new Circle0(); // a object in my library with linkage shapes.Circle
_circle.name = String("circle"+i);
_circle.id = i;
_circle.restitution = 0.5;
_circle.bullet = true;
_circle.buttonMode = true;
_circle.addEventListener(MouseEvent.CLICK, mClick);
_world.addChild(_circle);
}
}
private function mClick(e:MouseEvent):void{
trace(e.currentTarget.id)
}
}
}
Thanks in advance.