View Full Version : RPG shooting
Darth Turtle
January 28th, 2008, 11:59 PM
I am trying to make an rpg game but i am having trouble figuring out what the code would be to shoot. because lots of tutorials that i've tried have the bullets shoot in one direction and i want the bullet to shoot from where my main character is facing.
therobot
January 29th, 2008, 08:06 PM
This is by no means the best way to do it, but it's probably an easy way for you to understand. I hope you get it.
//just make sure that when the character changes direction,
//be it via key press or onEnterFrame or whatever,
//that you change the variable char.dir to
//the appropriate direction...i.e. "left" or "right"
onMouseDown = function ()
{
shoot(char.dir);
}
function shoot (facingdirection)
{
var bullet = char.attachMovie("bullet", "b" + char.getNextHighestDepth(), char.getNextHighestDepth());
if (facingdirection == "left")
{
bullet.onEnterFrame = function()
{
this._x--;
//this is where you'd want to check to see if it hits something.
}
} else {
bullet.onEnterFrame = function()
{
this._x++;
//this is where you'd want to check to see if it hits something.
}
}
}
Darth Turtle
February 6th, 2008, 12:55 AM
hmmm..
i kind of get it but i want to make it so that when you press Space, you shoot. Would i just change it to
onKeyPress(Key.SPACE) = function ()
{
shoot(char.dir);
}
??
And the code that you wrote is just the code to shoot right and left. Right?
parkerdc
February 6th, 2008, 02:04 AM
The extra code written: Moves (shoots) the bullet across the screen. It is passed a direction (left or right).
Darth Turtle
February 6th, 2008, 11:33 AM
^
?????
i have no clue what you just said. :huh:
therobot
February 6th, 2008, 03:12 PM
You've got the right idea, darth.
Parker was just saying the shoot function is passed a variable, in this case it's char.dir. If you look at the code, the first line of that function is:
function shoot(facingDirection)
Whereas when you call the function, as in shoot(char.dir), the variable dir is passed into the shoot function, so in the code for the function shoot, you can substitute the var "facingDirection" with the dir variable.
Your final code will look something like this:
onKeyPress(Key.RIGHT) = function ()
{
char.dir = "right";
//insert code to move character here.
}
onKeyPress(Key.LEFT) = function ()
{
char.dir = "left";
//insert code to move character here.
}
onKeyPress(Key.SPACE) = function ()
{
shoot(char.dir);
}
function shoot (facingdirection)
{
var bullet = char.attachMovie("bullet", "b" + char.getNextHighestDepth(), char.getNextHighestDepth());
if (facingdirection == "left")
{
bullet.onEnterFrame = function()
{
this._x--;
//this is where you'd want to check to see if it hits something.
}
} else {
bullet.onEnterFrame = function()
{
this._x++;
//this is where you'd want to check to see if it hits something.
}
}
}
Darth Turtle
February 7th, 2008, 10:51 PM
So the code for making it up and down would be
onKeyPress(Key.UP) = function ()
{
char.dir = "up";
//insert code to move character here.
}
onKeyPress(Key.DOWN) = function ()
{
char.dir = "down";
//insert code to move character here.
}
onKeyPress(Key.SPACE) = function ()
{
shoot(char.dir);
}
function shoot (facingdirection)
{
var bullet = char.attachMovie("bullet", "b" + char.getNextHighestDepth(), char.getNextHighestDepth());
if (facingdirection == "down")
{
bullet.onEnterFrame = function()
{
this._y++;
//this is where you'd want to check to see if it hits something.
}
} else {
bullet.onEnterFrame = function()
{
this._y--;
//this is where you'd want to check to see if it hits something.
}
}
}
bluemagica
February 8th, 2008, 12:04 PM
yes that is correct, but why are you making the left/right and up/down separately? also I would like to suggest two things:
1)Instead of char.dir="UP", "Left",... etc, use the values of the direction(0=right, 90=up, 180=left, 270=down) this helps in calculations if you need in the future.
2)onKeyPress is doing what you need but it is not checking against conditions, i mean, if the player presses both up and down at the same time the game will get confused, so it is better to use an if statement to make sure only a single key, or a key combo that you want, is pressed at a time.
If you have any questions or need help with these, feel free to ask.
Darth Turtle
February 12th, 2008, 08:05 PM
hmm... i kind of get what you are saying. so what would the code be if it was all together?
bluemagica
February 12th, 2008, 08:51 PM
err i dont feel like typing much so heres a glimpse...
if(Key.isDown(Key.LEFT))
{
this.dir=180
if (!this.hitTest(_x-4,y,0))
{
_x-=4;
}
}
else if(Key.isDown(Key.RIGHT))...
....
......
if(Key.isDOWN(Key.SPACE))
{
shoot(dir)
}
function shoot(bul_dir)
{
bullet=this.attachMovie("bullet", "b" + this.getNextHighestDepth(), this.getNextHighestDepth());
switch(bul_dir)
{
case 180:bullet.onEnterFrame=function()
{
_rotation=180
_x-=6
}
break;
case 0:.....
........
.......
}
}
Darth Turtle
February 13th, 2008, 12:59 PM
I dont really understand everything in that code like
this.dir=180
if (!this.hitTest(_x-4,y,0))
{
_x-=4;
}
}
or
{
case 180:bullet.onEnterFrame=function()
{
_rotation=180
_x-=6
}
break;
case 0:.....
........
.......
}
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.