""

Archive for February, 2011

I just came across this strange problem while writing a demo for car racing game on AS3. I don’t know how to start but please bear with me.
I have a button movieclip on a stage. It’s a “Play Game” button. When I click this button my game starts. So I press the button and game starts (also button is removed from the stage). Now I should be playing the game. But I could not play it. Why? Because my key press events are not getting fired, so I can’t drive my car. I had added the key listeners to stage but my events don’t respond. Then I mouse clicked on stage then viola my key press events are getting fired and suddenly I am able to drive my car. I scratch my head. Question marks appear over my head. I didn’t have the clue why this was happening? Here is the part flow of game code:

buttonClicked(){
    initGame();
    startGame();
}
initGame(){
    //init codes
    //adding keylisteners to stage
    //...........................................
}

So that is the flow. So when i enter startGame then somehow i am losing the focus on my stage since i am not able to get any response from any key events. But when i click on game screen/stage then my key events are working.
I didn’t found the valid reason behind it. I tried many things but couldn’t solve it. I googled it and found a simple solution on some forum. Solution is:

startGame(){
     stage.focus=stage;
     //.................................................start game codes.................
}

“stage.focus=stage” was the solution i found but i still haven’t figured out the reason behind this stupid problem. What i could conclude is that my focus is still with the button that i had pressed and focus hasn’t bubble up back to stage. So anyone who knows the reason, please do explain it.

Gracias

After hours of hard work finally i made my pinball flipper working properly with box2d flash. Yeah i had a pain of time while making my flipper work properly 😀

So before writing points on how i made my pinball flipper, i wanted to make clear that this is the way i did it so it may not be better way. If anyone has got better technique then please pretty please share with us hehe 😀 Thanks.

So first thing first, i created a rectangle i.e polygon set as box, this rectangle was my pinball flipper. Now i wanted to move/rotate the flipper within given limits as it does in any pinball game. Yeah i didn’t mention that i chose box2d flash as it supports all my physics needs and does its job pretty good so i would not have to write the collision and basic physics from scratch.

Now next step, i had to rotate flipper around a pivot point near the end of flipper, by default my flipper would rotate around its center which i didn’t wanted to happen. Second, i wanted to make my flipper aka polygon to be dynamic body so that it would react to gravity. So if i made my flipper dynamic then my whole flipper would fall down acting to gravitational force, this i didn’t wanted to happen either. So i needed a way to fix my flipper to a pivot point around which my flipper will rotate and also my flipper would react to gravity provided flipper should be attached to that pivot point. I hope you understand what i am saying 😀
Next step, i had to restrict the rotation of flipper because i didn’t wanted my flipper to cover whole 360 degree circular motion.

Well now the solution of above, i would say again that i had a pain in my ___ to make it work. But when i found the solution and implemented it, it looked easy and smooth like butter or cheese hehe 😀

Ok so what i did is i created a small circle which looked like a dot, this circle dot will act as my pivot point. Now my polygon flipper will be attached to this circle dot, which is static body, it means my circle dot would not react to gravity. So coming to bottom line, i created a revolute joint between the circle dot and polygon flipper. With this, my circle dot will act like a hinge and flipper will be rotating around that hinge and also flipper would react to gravity. Now just restriction for rotation of flipper is left which is done by setting lowerAngle and upperAngle property of revolute joint. Another important thing is setting enableLimit to true.

I assume you know Box2d so not explaining all codes in details. Now code of setting up whole flipper and hinge pivot point:

sawDef=new b2BodyDef();
sawDef.type=b2Body.b2_dynamicBody;
sawDef.position.Set((Metrics.HALF_WIDTH+40)/Metrics.PIXELS_TO_METER,18);
			
sawBody=world.CreateBody(sawDef);
			
var sawBox:b2PolygonShape=new b2PolygonShape();
sawBox.SetAsBox(1.1,0.2);
			
var sawfixtureDef:b2FixtureDef=new b2FixtureDef();
sawfixtureDef.shape=sawBox;
sawfixtureDef.density=2;
sawfixtureDef.friction=0.;
sawBody.CreateFixture(sawfixtureDef);
			
localCenter = sawBody.GetWorldCenter();
localCenter.Add(new b2Vec2(25/Metrics.PIXELS_TO_METER,0));
			
var circleBodyDef:b2BodyDef= new b2BodyDef();
circleBodyDef.position.Set(localCenter.x,localCenter.y);
circleBodyDef.type=b2Body.b2_staticBody;
			
var my_circle:b2CircleShape=new b2CircleShape(1/Metrics.PIXELS_TO_METER);
var my_fixture:b2FixtureDef = new b2FixtureDef();
my_fixture.shape=my_circle;
var circle_body:b2Body=world.CreateBody(circleBodyDef);
circle_body.CreateFixture(my_fixture);
			
revoluteJointDef = new  b2RevoluteJointDef();
revoluteJointDef.Initialize(sawBody, circle_body, localCenter);
revoluteJointDef.upperAngle = .6;
revoluteJointDef.lowerAngle = -.6;
revoluteJointDef.enableLimit = true;
revoluteJointDef.maxMotorTorque = 10.0;
revoluteJointDef.motorSpeed = 0.0;
revoluteJointDef.enableMotor = true;
world.CreateJoint(revoluteJointDef);

Note: Metrics.PIXELS_TO_METER is 30 (in box2d length is measured in meters)

I have created a polygon body(sawbody) and small circle as hinge point (circle_body). Then i am creating a revolute joint between polygon and circle by fixing circle near the end of polygon represented by variable localCenter(b2vec2). Now i want to set angle limit for the rotation of flipper say 35 degree, it means flipper will cove only 35 degree rotation. So i am setting upper angle and lower angle to 0.6 and -0.6 respectively because it takes radians as value and 35 degree is equal to approx 0.6 radians. Now you have to set the enableLimit to true.

Good posts on Revolute Joint: Emanuele’s and Allan Bishop’s

Here is the code how i am using the flipper:

private function update(event:Event):void {
	var timeStep:Number = 1 / 30;
	var velocityIteration:int = 6;
	var positionIteration:int = 2;
			
	world.Step(timeStep, velocityIteration, positionIteration);
	world.ClearForces();
	world.DrawDebugData();
			
	input();
}

private function input() {
		if (RIGHT) {
			sawBody.ApplyTorque(150);
		}
		else if (!RIGHT) {
			if(sawBody.IsAwake()){
					sawBody.ApplyTorque( -150);
			}
		}
			
		if (LEFT) {
			sawBody2.ApplyTorque(-150);
		}
		else if (!LEFT) {
			if (sawBody2.IsAwake()) {
				sawBody2.ApplyTorque(150);
			}
		}
	}

I hope this posts helped u. If u have more good techniques then please share.

Happy Valentines Day
Día del Amor y la Amistad
Gracias