Click here to Skip to main content
15,886,799 members
Articles / Adobe / AS3

Killer Instinct

Rate me:
Please Sign up or sign in to vote.
4.11/5 (2 votes)
22 Oct 2012CPOL 12K   9   1  
Killer Instinct is physics based shooting game. The objective is to perfectly shoot the enemies.

Sample Game Image

Introduction

Killer Instinct is a physics based shooting game. The objective is to perfectly shoot the enemies. The game consists of a character holding a gun and the enemies standing at their positions. The obstacles can stop the hero's bullets or fall over the hero. This game is written in AS3 using physics game engine called Box2D.

  • Fully playable with mouse, and touch.
  • Bullets are projected from the gun with certain speed and it repels when it hits to any physics bodies or walls.
  • Hero and enemy characters behaves as rag-dolls.

Background

Interest towards shooting games made me think of this. We planned to add physics to make the game interesting.

Using the Code

The back end of the game is built in AS3, making use of physics based engine Box2D. The main aspect of the game is to move the bullet.

JavaScript
//Bullet Creation:

public function BulletActor(parent,xValue,yValue,rotation,vx,vy)
{    
    skin = new Arrow();
    skin.x = xValue;
    skin.y = yValue;
    skin.rotation = rotation*180/Math.PI;
    parent.addChild(skin);
    prevX = skin.x; 
    prevY = skin.y; 

    var shape:b2PolygonShape = new b2PolygonShape();
    shape.SetAsBox(6 / Phyvalues.RATIO, 1.4 / Phyvalues.RATIO);
    var fixtureDef:b2FixtureDef= new b2FixtureDef();
    fixtureDef.density = 20;
    fixtureDef.restitution = 1;
    fixtureDef.friction = 0;
    fixtureDef.userData = skin;
    fixtureDef.userData.remove = remove;
    fixtureDef.userData.prevX = prevX;
    fixtureDef.userData.prevY = prevY;
    fixtureDef.userData.count = count;
    fixtureDef.shape = shape;
    fixtureDef.filter.groupIndex = -1;            
    var bodyDef:b2BodyDef = new b2BodyDef();
    bodyDef.bullet = true;
    bodyDef.angle = rotation;
    bodyDef.type = b2Body.b2_dynamicBody;
    bodyDef.position.Set(xValue / Phyvalues.RATIO, yValue / Phyvalues.RATIO);
    bodyDef.fixedRotation = true;
    body = Phyvalues._world.CreateBody(bodyDef);
    body.CreateFixture(fixtureDef);

    body.SetLinearVelocity(new b2Vec2(vx, vy));
    body.SetBullet(true);
    body.applyGravity = false;
    super(body, skin);
}    

Bullet Movement:


private function loop(e:Event):void
{
    if (_costume is Bullet) {
    var dx = _costume.x - _costume.prevX;
    var dy = _costume.y - _costume.prevY;
    var ang = Math.atan2(dy, dx);
    _body.SetAngle(ang);
    _costume.prevX = _costume.x;
    _costume.prevY = _costume.y;
    _costume.remove++;
    var force:b2Vec2 = _body.GetLinearVelocity();
    _body.ApplyForce(force, _body.GetWorldCenter());
    }
    _costume.x = _body.GetPosition().x * Phyvalues.RATIO;
    _costume.y = _body.GetPosition().y * Phyvalues.RATIO;
    _costume.rotation = _body.GetAngle() * 180 / Math.PI;
}

//When Bullet hits to enemy character ragdolls are created
//and force is applied to them by according to bullet force.

//Rag doll Creation:
    var xpt = Phyvalues._allActors[i].xv;
    var ypt = Phyvalues._allActors[i].yv;
    var vx = Phyvalues._allActors[i].velocity.x;
    var vy = Phyvalues._allActors[i].velocity.y;
    ragDolls = new RagDolls1(xpt, ypt); //ragdolls generating.

    // applying force to ragdoll.
    ragDolls.body.ApplyImpulse(new b2Vec2(vx, vy), ragDolls.body.GetWorldCenter()); 


...

Points of Interest

Creation of ragdolls and their movement.

History

  • 22nd October, 2012: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --