View Full Version : [CS2] Making a stickman shoot
PhatKitty
02-13-2009, 12:41 AM
I'm making a platform game, and I've managed to do jump, pickup items, walking animation all of that, but I want to know how to shoot my guns. In all the tutorials I can find they are always for shooting in one direction (either left or right) or shooting with the mouse, neither I want. I want it so if I press space and my char is facing left, he shoots left, right he shoots right. It seems so simple, but I cannot find it anywhere, a link or any help would be greatly appreciated.
I'm using this for moving my char.
if (Key.isDown(65)) {
_xscale = -100;
this._x -= 30;
this.gotoAndStop(2);
} else if (Key.isDown(68)) {
_xscale = +100;
this._x += 30;
this.gotoAndStop(2);
this.gotoAndStop(2) is the frame of my char running.
Cauterize
02-13-2009, 11:02 AM
EDIT: Apologies, I wrote this as if i was coding in AS3. If you can convert it, then it should be fine
Use Booleans to check which way the player is facing.
var facingLeft:Boolean = false;
var facingRight:Boolean = false;
Next set a variable for the Bullet speed and a variable for shooting.
var bulletSpeed:Number = 5;
var shotBullet:Boolean = false;
Then use something like this
if (shotBullet == true && facingLeft == true)
{
bullet.x -= bulletSpeed
shotBullet = false;
}
if (shotBullet == true && facingRight == true)
{
bullet.x += bulletSpeed
shotBullet = false;
}
Obviously this isnt the right code exactly, just the idea behind it. Youll need the bullet movement in an ENTER_FRAME to keep it moving.
This is a real basic idea/example and im not the best at AS3 yet, but hopefully it will help!
PhatKitty
02-13-2009, 05:52 PM
Thanks, I'll try this and use the shooting script for a space ship shooting agme, hopefuly it'll work :C
PhatKitty
02-14-2009, 06:07 PM
Finally it works!
var facingLeft:Boolean = false;
var facingRight:Boolean = false;
var i = 0;
var ii = 0;
var shotBullet:Boolean = false;
onEnterFrame = function () {
if (Key.isDown(68)) {
facingLeft = true;
facingRight = false;
}
if (Key.isDown(65)) {
facingRight = true;
facingLeft = false;
}
if (Key.isDown(Key.SPACE)) {
shotBullet = true;
} else {
shotBullet = false;
}
if (shotBullet == true && facingLeft == true) {
i++;
_root.attachMovie("Bullet", "Bullet"+i, _root.getNextHighestDepth());
_root["Bullet"+i]._x = _root.char._x+120;
_root["Bullet"+i]._y = _root.char._y-130;
}
if (shotBullet == true && facingRight == true) {
ii++;
_root.attachMovie("Bullet_2", "Bullet_2"+ii, _root.getNextHighestDepth());
_root["Bullet_2"+ii]._x = _root.char._x-120;
_root["Bullet_2"+ii]._y = _root.char._y-130;
}
};
But one small problem left, I want to change this bit
_root["Bullet"+i]._x = _root.char._x+120;
_root["Bullet"+i]._y = _root.char._y-130;
Into
_root["Bullet"+i]._x = gun_inside._x+120;
_root["Bullet"+i]._y = gun_inside._y;
gun_inside is the name for the gun in the frame that all the script is in.
But when I change it, nothing happens.
Also, how do I make it so when I exit the frame, the function is cleared?
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.