Welcome, Guest. Please Login or Register.  • Help
SMF Underground
+ SHMUP-DEV » SHMUP DEV FORUMS » Assistance
|-+ Simple bullet shooted towrd enemy (AS 3)

Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read July 08, 2011, 10:34:24 PM #0
fracedo

Simple bullet shooted towrd enemy (AS 3)

Hello i'm new to the forum, and i'm making a shoot em up in Actionscript 3.

The problem i having right now, is that i want an enemy ship ship shoot toward the main ship, like many games.

i don't want the enemy bullet follow the main ship after it moves (like a homing missile), only in the beginning.

I want the bullet make a straight path.

If i wasn't clear i can make an visual example of what i want to achieve.

Sorry for my english i speak spanish Tongue.
Offline  
Read July 09, 2011, 04:51:20 PM #1
fracedo

Re: Simple bullet shooted towrd enemy (AS 3)

I think i solved it, i post the code if anyone need this Cheesy.

First the code separated into two functions:
-The first function (createSmallBullet) is the function that create the bullet
-The second function is an auxiliar to obtain the angle between two points (Our main ship and the bullet)

Code:

public function createSmallBullet(levelContainerP:Sprite,bulletsEnemy:Array,mainShip:MainShip,positionX:Number,positionY:Number,bulletSpeed:Number)
{
var bulletToCreate:EnemySmallBullet= new EnemySmallBullet();
bulletToCreate.x=this.x+positionX;
bulletToCreate.y=this.y+positionY;
bulletToCreate.gotoAndStop(1);

var angle:Number;
var dirX:Number;
var dirY:Number;

angle=getAngle(mainShip,bulletToCreate);
dirX = Math.cos(angle*Math.PI/180)*bulletSpeed;
dirY = Math.sin(angle*Math.PI/180)*bulletSpeed;


bulletToCreate.setSpeed(dirX,dirY);
levelContainerP.addChild(bulletToCreate);


bulletsEnemy.push(bulletToCreate);
}

create small bullet expects:
-levelContainer an place to add the child (movieclip)
-bulletsEnemy a list to manage the bullet after the creation
-mainShip the main ship
-position x and y if the bullet is not created in the 0,0 of the enemy movieclip

first we create our bullet with x and y, then we make it stop in 1 (this is used when the bullet have an explode animation)

we get the angle (explained later)
the dirX,dirY use trigonometry to know where to go when is created based on the angle obtained (well explained in Emanuele Feronato site).

the setSpeed is just two Numbers to increment each step in the game loop, we pass the directions obtained before.
something like:
(in the game loop)
x+=speedX;
y+=speedY;

finally we add the child and put it in an array, to handle it and destroy it when it's not longer needed.

Code:
public function getAngle(mainShip:MainShip,bullet:EnemySmallBullet):Number
{
var angle:Number;

var shipPositionX:Number;
var shipPositionY:Number;
shipPositionX = (mainShip.x+mainShip.OFFSET_COLLISION_X)-bullet.x;
shipPositionY = ((mainShip.y+mainShip.OFFSET_COLLISION_Y)-bullet.y)*-1;
angle = Math.atan(shipPositionY/shipPositionX)/(Math.PI/180);
if (shipPositionX<0) {
angle += 180;
}
if (shipPositionX>=0 && shipPositionY<0) {
angle += 360;
}

angle = angle *-1

return angle;
}

Ok, here it's the function to get the angle.
shipPositionX is used to determinate the space between the main ship and the enemy bullet.
the same with the y.
the OFFSET_COLLISION_X is used because i have an rectangle movieclip to check the collision and because this movieclip itīs not in the origin 0,0, i added the offset to the x and y of the main ship.

get the angle explained here:
http://www.emanueleferonato.com/2007/02/17/create-a-flash-draw-game-like-line-rider-or-others-part-3/

and the angle * -1 rotate the bullet according to the angle.

The code is based on emanuele feronato tutorial to "Create a flash artillery game" http://www.emanueleferonato.com/2007/04/28/create-a-flash-artillery-game-step-1/

Hope it helps Cheesy.
Offline  
Pages: [1]   Go Up
Jump to:  

Page created in 0.057 seconds with 17 queries.