My second problem is that i want to make something like an elevator and the circle should stay on it when the plattform is moving. Problem is i cant to do that by adding more gravity, because if i do that i can make the ball jump from one to the next later on in my whole flash. Anyone maybe have any idea what else i could do?
Code:
package shapetest {
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite {
private var _world:b2World;
private var _worldScale:int = 30;
private var _shape1:b2Body;
private var _box:b2Body;
private var _direction:String = "up";
private function debugDraw():void {
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.SetSprite(debug_sprite);
debug_draw.SetDrawScale(this._worldScale);
debug_draw.SetFillAlpha(0.5);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
this._world.SetDebugDraw(debug_draw);
}
private function update(evt:Event):void {
this._world.Step(1/30,10,10);
this._world.ClearForces();
this._world.DrawDebugData();
var cP:b2Vec2 = this._box.GetPosition();
if( this._direction == "up" ) {
this._box.SetPosition( new b2Vec2(275/this._worldScale,cP.y-0.1) );
} else {
this._box.SetPosition( new b2Vec2(275/this._worldScale,cP.y+0.1) );
}
}
private function onTimerEvent(evt:TimerEvent):void {
this._direction == "up" ? this._direction = "down" : this._direction = "up";
}
public function Main() {
this._world = new b2World( new b2Vec2(0.0, 10.0), true );
var my_body:b2BodyDef;
var my_circle:b2CircleShape;
var my_fixture:b2FixtureDef;
var my_box:b2PolygonShape;
// shape1
my_body = new b2BodyDef();
my_body.position.Set( 275/this._worldScale, 70/this._worldScale );
my_body.type = b2Body.b2_dynamicBody;
my_circle = new b2CircleShape( 40/_worldScale );
my_fixture = new b2FixtureDef();
my_fixture.shape=my_circle;
my_fixture.friction = 1;
my_fixture.density = 0.5;
my_fixture.restitution = 0.0;
this._shape1 = this._world.CreateBody( my_body );
this._shape1.CreateFixture( my_fixture );
this._shape1.SetSleepingAllowed( false );
// box
my_body= new b2BodyDef();
my_body.position.Set(275/this._worldScale, 300/this._worldScale);
my_body.type=b2Body.b2_staticBody;
my_box = new b2PolygonShape();
my_box.SetAsBox(275/2/this._worldScale, 10/2/this._worldScale);
my_fixture = new b2FixtureDef();
my_fixture.shape=my_box;
my_fixture.friction = 0.2;
my_fixture.density = 1;
my_fixture.restitution = 0;
this._box=this._world.CreateBody(my_body);
this._box.CreateFixture(my_fixture);
var timer:Timer = new Timer(2000);
timer.addEventListener( TimerEvent.TIMER, this.onTimerEvent );
timer.start();
this.debugDraw();
this.addEventListener(Event.ENTER_FRAME, this.update);
}
}
}
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite {
private var _world:b2World;
private var _worldScale:int = 30;
private var _shape1:b2Body;
private var _box:b2Body;
private var _direction:String = "up";
private function debugDraw():void {
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.SetSprite(debug_sprite);
debug_draw.SetDrawScale(this._worldScale);
debug_draw.SetFillAlpha(0.5);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
this._world.SetDebugDraw(debug_draw);
}
private function update(evt:Event):void {
this._world.Step(1/30,10,10);
this._world.ClearForces();
this._world.DrawDebugData();
var cP:b2Vec2 = this._box.GetPosition();
if( this._direction == "up" ) {
this._box.SetPosition( new b2Vec2(275/this._worldScale,cP.y-0.1) );
} else {
this._box.SetPosition( new b2Vec2(275/this._worldScale,cP.y+0.1) );
}
}
private function onTimerEvent(evt:TimerEvent):void {
this._direction == "up" ? this._direction = "down" : this._direction = "up";
}
public function Main() {
this._world = new b2World( new b2Vec2(0.0, 10.0), true );
var my_body:b2BodyDef;
var my_circle:b2CircleShape;
var my_fixture:b2FixtureDef;
var my_box:b2PolygonShape;
// shape1
my_body = new b2BodyDef();
my_body.position.Set( 275/this._worldScale, 70/this._worldScale );
my_body.type = b2Body.b2_dynamicBody;
my_circle = new b2CircleShape( 40/_worldScale );
my_fixture = new b2FixtureDef();
my_fixture.shape=my_circle;
my_fixture.friction = 1;
my_fixture.density = 0.5;
my_fixture.restitution = 0.0;
this._shape1 = this._world.CreateBody( my_body );
this._shape1.CreateFixture( my_fixture );
this._shape1.SetSleepingAllowed( false );
// box
my_body= new b2BodyDef();
my_body.position.Set(275/this._worldScale, 300/this._worldScale);
my_body.type=b2Body.b2_staticBody;
my_box = new b2PolygonShape();
my_box.SetAsBox(275/2/this._worldScale, 10/2/this._worldScale);
my_fixture = new b2FixtureDef();
my_fixture.shape=my_box;
my_fixture.friction = 0.2;
my_fixture.density = 1;
my_fixture.restitution = 0;
this._box=this._world.CreateBody(my_body);
this._box.CreateFixture(my_fixture);
var timer:Timer = new Timer(2000);
timer.addEventListener( TimerEvent.TIMER, this.onTimerEvent );
timer.start();
this.debugDraw();
this.addEventListener(Event.ENTER_FRAME, this.update);
}
}
}