flash on 2010-6-1
/**
* Copyright Kronoshifter ( http://wonderfl.net/user/Kronoshifter )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/A6RN
*/
package
{
import flash.display.*;
import flash.events.*;
import com.actionsnippet.qbox.*;
import Box2D.Common.Math.*;
public class Warp extends MovieClip
{
private var grounded:Boolean;
private var sim:QuickBox2D;
private var main:QuickObject;
private var contacts:QuickContacts;
private var keyArray:Array;
private var speed:int;
private var jumpPower:int;
public function Warp()
{
//set the player's ability to jump
grounded = false;
//instantiate the QuickBox2D class
sim = new QuickBox2D(this);
//set a few defaults
sim.setDefault({lineAlpha: 0, fillColor: 0x333333});
//create boxes around the stage
sim.createStageWalls();
//create platforms, etc
sim.addBox({x:400 / 30, y: 13, width: 4, height: 4, angle: Math.PI / 4, density: 0});
sim.addBox({x: 4, y: 11, width: 8, height: .5, density: 0});
sim.addBox({x: 0, y: 9, width: 3, height: 3, angle: Math.PI / 4, density: 0});
//instantiate the player's object
main = sim.addCircle({x: 3, y: 3, lineAlpha: 1,
fillColor: 0x888888,
allowSleep: false, fixedRotation: true});
//start the simulation
sim.start();
//instantiate a new contact listener
contacts = sim.addContactListener();
//add event listeners for the QuickContacts events
contacts.addEventListener(QuickContacts.ADD, onAdd);
contacts.addEventListener(QuickContacts.PERSIST, onPersist);
contacts.addEventListener(QuickContacts.REMOVE, onRemove);
//add event listeners for framerate code and player input
stage.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
//create a new array in keyArray
keyArray = new Array();
//set the movement speed and jumping power of the player
speed = 5;
jumpPower = -10;
for (var i:int = 0; i < 222; i++)
{
keyArray.push(false);
}
}
//handler for the QuickContacts ADD event
private function onAdd(e:Event):void
{
if (contacts.currentPoint.shape1 == main.shape || contacts.currentPoint.shape2 == main.shape)
{
if (contacts.currentPoint.normal.y >= -1 && contacts.currentPoint.normal.y < 0)
{
grounded = true;
}
}
}
//handler for the QuickContacts PERSIST event
private function onPersist(e:Event):void
{
if (contacts.currentPoint.shape1 == main.shape || contacts.currentPoint.shape2 == main.shape)
{
if (contacts.currentPoint.normal.y >= -1 && contacts.currentPoint.normal.y < 0)
{
grounded = true;
}
}
}
//handler for the QuickContacts REMOVE event
private function onRemove(e:Event):void
{
grounded = false;
}
//handler for the ENTER_FRAME event
private function loop(e:Event):void
{
//RIGHT ARROW KEY
if (keyIsDown(39))
{
main.body.SetLinearVelocity(new b2Vec2(speed, main.body.GetLinearVelocity().y));
}
//LEFT ARROW KEY
if (keyIsDown(37))
{
main.body.SetLinearVelocity(new b2Vec2(-speed, main.body.GetLinearVelocity().y));
}
//SPACE BAR
if (keyIsDown(32) || keyIsDown(38))
{
//check to see if the player is touching the ground
if (grounded)
{
main.body.SetLinearVelocity(new b2Vec2(main.body.GetLinearVelocity().x, jumpPower));
}
}
}
private function checkKeysDown(event:KeyboardEvent):void
{
keyArray[event.keyCode] = true;
}
private function checkKeysUp(event:KeyboardEvent):void
{
keyArray[event.keyCode] = false;
}
private function keyIsDown(X:Number):Boolean
{
return keyArray[X];
}
}
}