SOLVED Help with projectiles.

sweep

Member
Hope someone can help. I've set up a player who can throw a dagger . The code is below. But in the game the player throws multiple daggers. I only want to show one dagger . I've tried to figure it out but without much luck. I know using two dagger images (one facing left/ one right) isn't the correct way to keep the dagger traveling in the players direction and the right way around, but changing the image_xscale caused more problems.

( the button i am using is keyboard_check_pressed(vk_alt) )

function scr_playerstaterthrow()
{
sprite_index = spritethrow;
image_speed = 1;
global.daggeramount = -1

with(instance_create_layer(x,y-9,"player",obj_dagger))
{
speed = 10

if (obj_player.image_xscale = 1)
{
image_speed = 0
image_index = 0
direction = 0
}
else
{
direction = 180;
image_speed = 0
image_index = 1
}
}

}
 
Last edited:

curato

Member
I like to use released rather than pressed personally. I have had a better experience with it not triggering multiple times.
 

sweep

Member
It didn't work for me but thanks for the suggestion. Also, i don't know if this is a result of the multiple objects, sometimes the dagger gets stuck inside the wall object even with collision.

Screen Shot 2020-12-22 at 22.42.49.png
 

kburkhart84

Firehammer Games
1. Have you tried something other than vk_alt?

2. Where is the code for the actual button checking? You only show the part that creates the thing but not the button checking code.

I like to use released rather than pressed personally. I have had a better experience with it not triggering multiple times.
Maybe its just me, but I've never had issues with "pressed" triggering multiple times.
 

sweep

Member
1. Have you tried something other than vk_alt?

2. Where is the code for the actual button checking? You only show the part that creates the thing but not the button checking code.
the button check is in another script for when the player is moving around key_throw = keyboard_check_pressed(vk_alt).
I've tried all different keys and get the same result or nothing happens.
code in move script
if (key_throw) && global.daggeramount > 0
{
state = scr_playerstaterthrow;
}
 
Last edited:

kburkhart84

Firehammer Games
I think I may know your issue, but not for sure. Do you have any code that takes you back OUT of the scr_playerstaterthrow? The code you posted doesn't change the state, so if each step you are calling that function since you are still in that state, it repeats.

A solution many people do is to have a quick single frame state for throwing, and then have that state take you to another state that is for finishing up the animation of the throw, recovering from it, etc... and then when that state is done, go back to the original state.
 

sweep

Member
I have this code in the player animation end event. the state moves fine between throwing and moving. The player completes the throw animation but about ten dagger fly out instaed of one.

if state == (scr_playerstatethrow)
{
state = scr_playerstatefree;
}
 

kburkhart84

Firehammer Games
So are you still calling the scr_playerstatethrow() function every step until that animation end event? If you are, that't the problem. You could put a simple variable in there to say you had already thrown the dagger. Set it to false when you first create the object, then if its true, throw the dagger(while in that player state of throwing). Finally, set it back to false in that animation end event. This will let the rest of the state's code run if you code the condition correctly.
 

sweep

Member
Added thrown dagger variable, the code looks like this. Still had multi daggers . Not sure if this has anything to do with the problem, the dagger
gets thrown only if iam standing near a wall ?? The animation plays with multiple daggers in front of the player but they don't fly across the room.


scr_playerstatefree ( Accessed through the player step event)

if (key_throw) && global.daggeramount > 0 &&
{
state = scr_playerstaterthrow;
}
---------------------------------------
scr_playerstetethrow

function scr_playerstaterthrow()
{
sprite_index = spritethrow;
image_speed = 1;
global.daggeramount = -1


with(instance_create_layer(x,y-9,"player",obj_dagger))
{
speed = 10

if (obj_player.image_xscale = 1)
{
image_speed = 0
image_index = 0
direction = 0
}
else
{
direction = 180;
image_speed = 0
image_index = 1
}
}
}
.....................................................
player animation end event

if state == (scr_playerstateladder)
{
state = scr_playerstatefree;
throwndagger = false;
}
.....................................................
Dagger step event

if (place_meeting(x,y,obj_wall))
{
speed = 0;
}
 

Nidoking

Member
You're not performing any checks once you're in the throw state to determine whether or not to throw a dagger. You're throwing a new dagger every step until you transition to a different state. You need to put the logic in there if you want something to happen only under certain conditions. That's what an if DOES.

And why do you even have a throw state for something that you want to happen once, immediately? Why not just spawn the dagger when you push the key, and if you need a separate state to manage the animation, just have that state control the animation?
 

sweep

Member
I only want to throw the dagger once because if the dagger hits the wall, it will stop and the player will be able to pick it back up.
The player is throwing ten or more daggers during one animation. I only want to see one.
Could this be sprite problem as there ten daggers and the frame rate of the sprite is ten frames per second ?
Screen Shot 2020-12-23 at 12.30.53.png
 

Nidoking

Member
Go back and read the post I already made. Understand the information in it. There are no other answers for you no matter how many times you ask the same question. It's not the framerate. It's not the sprite. It's this:
You're not performing any checks once you're in the throw state to determine whether or not to throw a dagger. You're throwing a new dagger every step until you transition to a different state. You need to put the logic in there if you want something to happen only under certain conditions. That's what an if DOES.
 

kburkhart84

Firehammer Games
The reason 10 frames of animation correlates with 10 daggers is because it is throwing a dagger every frame. I see you added the variable I told you about, but like Nidoking says, you didn't add the if statement in the function to see if that variable is true/false before throwing the dagger, and you didn't set it to true anywhere once you have thrown the dagger...you only set it to false in one spot, but then you never use the thing.

And why do you even have a throw state for something that you want to happen once, immediately? Why not just spawn the dagger when you push the key, and if you need a separate state to manage the animation, just have that state control the animation?
This is also a perfectly valid point, though I was more interested in getting the logic right in the original code before worrying about optimizing it, as that seems to be a much bigger problem.
 

sweep

Member
Thank you very much for you help. The player now throws one dagger. Sorry if my posts came across as if I wasn't taking the information in I am new to programming this is the my first attempt at states with animations. Thanks again you have been a big help.
 
Top