Page 1 of 1

2 bullet actor

PostPosted: Fri Sep 13, 2013 8:22 am
by Behdadsoft
Hi.

I made a character that can shoot to 4 Direction and I made bullet actor for shoot. But I want change x, y position when the player is holding the gun up. Because I want Bullet out of the barrel of a gun.
Now I should make another bullet actor or can change x, y Position when the player is holding the gun up?

Please Guide Me.

Re: 2 bullet actor

PostPosted: Fri Sep 13, 2013 8:29 am
by skydereign
Don't create another bullet actor. In the event where you create the first bullet, add some code to create the actor in different positions depending on which direction the gun is facing.
Code: Select all
// something like this
switch(dir)
{
    case 0: // right
    CreateActor("bullet", "bullet", "(none)", "(none)", 10, 0, false);
    break;

    case 1: // up
    CreateActor("bullet", "bullet", "(none)", "(none)", 0,  -10, false);
    break;

     // and so on
}


Depending on the location of the barrel, you could simplify the code into one CreateActor call.

Re: 2 bullet actor

PostPosted: Fri Sep 13, 2013 10:09 am
by Behdadsoft
thanks

I use this code :

Code: Select all
// something like this
switch(right)
{
case 1: // right
CreateActor("White_Bullet", "White_Bullet", "(none)", "(none)",3.5, -4, false);
break;
}

switch (up)
{
case 1: // up
CreateActor("White_Bullet", "White_Bullet", "(none)", "(none)",2, -25, false);
break;
}


but when the Player is holding the gun up, two bullets out of the gun.

also I create actor on bullet actor and used this code:

Code: Select all
// Player shoot to Left & Right
if (right == 1 && left == 0) xvelocity += 7;
if (left == 1 && right == 0) xvelocity -= 7;

// Player shoot to UP
if (right == 1 && left == 0 && up == 1)
{
xvelocity = 0;
yvelocity -= 7;
}

if (left == 1 && right == 0 && up == 1)
{
xvelocity = 0;
yvelocity -= 7;
}

Re: 2 bullet actor

PostPosted: Fri Sep 13, 2013 10:17 am
by skydereign
I recommend only using one variable for direction. The problem is both those switch statements will create an actor if up and right are set to 1. You could use an if, else if, else statement to handle this, but cleaning up direction by putting it in one variable would be all around better.

Re: 2 bullet actor

PostPosted: Fri Sep 13, 2013 10:49 am
by Behdadsoft
even I used if statement but when I changed x, y bullet position don't change it.

Code: Select all

if ((right == 1 && left == 0) || (left == 1 && right == 0))
{
CreateActor("White_Bullet", "White_Bullet", "(none)", "(none)",3.5, -4, false);
}

else

if ((right == 1 && left == 0 && up == 1) || (left == 1 && right == 0 && up == 1))
{
CreateActor("White_Bullet", "White_Bullet", "(none)", "(none)",1, -10, false);
}

Re: 2 bullet actor

PostPosted: Fri Sep 13, 2013 11:01 am
by skydereign
That code is rather messy looking. It's hard to tell what direction you mean. Here's how it looks when simplified into pseudocode.
Code: Select all
if(right or left)
{
     create bullet at (3.5, -4)
}
else if ((up and right) or (up and left))
{
    create bullet at (1, -10)
}

Notice the else if part will never trigger. The first condition must be true for the second to ever trigger, and it is already caught by the first if. To do what you want, you would need to add up==0 checks to the first if. But again I don't recommend, as using one variable for direction will make your code much easier in the end.

Re: 2 bullet actor

PostPosted: Fri Sep 13, 2013 11:11 am
by Behdadsoft
Thanks skydereign It is much cleaner.
+1

my character have 8 direction how can use 1 variable ? I want improve it.

Re: 2 bullet actor

PostPosted: Fri Sep 13, 2013 11:26 am
by skydereign
It depends on how you handle directions. The general idea is you have one variable dir, that can have 8 values (0-7). Typically 0 is right, 1 is up right, 2 is up, and so on. When you press a key, you can change the dir variable depending on what the value already was via a switch statement.

Re: 2 bullet actor

PostPosted: Fri Sep 13, 2013 11:31 am
by Behdadsoft
this is an demo of my game:

right arrow = move to right
left arrow = move to left
up arrow = look up
down arrow = sit
space arrow = jump
right + up = mid right up
left + up = mid left up
right + down = mid right down
left + down = mid left down
x = shoot

https://www.dropbox.com/s/zsf9hlxsqzf9yjn/Contra.exe

i have some problem if you can help me.