How to destroy a moving bullet?

Get help using Construct 2

Post » Thu Oct 06, 2016 7:02 am

Hi guys, I'm trying to whenever i touches the screen, the bullet will be spawn from the player, and the bullet will be destroyed on where I touch. To do that, I tried

Touch, on any touch start
- Player spawn bullet
- set x (instance variable) to Touch.X
- set y (instance variable) to Touch.Y
- Bullet set angle towards (Touch.X, Touch.Y)

Sub-event
- Bullet X = x and Bullet Y = y, destroy bullet

Below is my capx file
https://www.dropbox.com/s/56io2ckgi36qf ... .capx?dl=0
Last edited by johnkx on Thu Oct 06, 2016 7:56 am, edited 1 time in total.
B
48
S
20
G
6
Posts: 73
Reputation: 6,474

Post » Thu Oct 06, 2016 7:33 am

Kind of obvious (but I don't blame your possible inexperience) but did you try adding on tap to the sub-event?
Zelda Gamekit (my project)



Example capxs:

Zelda-Style Inventory

Zelda Health

3-saveslot system

[url=https://www.dropbox.com/s/d4396pwak0oh7q6/ZeldaDialogueSystem.capx?dl=0]Zelda-like Dialogue[
B
51
S
24
G
30
Posts: 534
Reputation: 19,620

Post » Thu Oct 06, 2016 7:54 am

@LaDestitue I'm sorry, what do you mean "on tap"?
B
48
S
20
G
6
Posts: 73
Reputation: 6,474

Post » Thu Oct 06, 2016 8:04 am

The simplest way to do this is on any touch create a different invisible object for detection at touch.x, touch.y and destroy sprite2 when overlapping the created object.
B
47
S
17
G
81
Posts: 2,225
Reputation: 48,097

Post » Thu Oct 06, 2016 8:06 am

It's in the actions for Touch controls. It's a trigger condition for Touch that says "On tap"
Zelda Gamekit (my project)



Example capxs:

Zelda-Style Inventory

Zelda Health

3-saveslot system

[url=https://www.dropbox.com/s/d4396pwak0oh7q6/ZeldaDialogueSystem.capx?dl=0]Zelda-like Dialogue[
B
51
S
24
G
30
Posts: 534
Reputation: 19,620

Post » Thu Oct 06, 2016 8:18 am

@plinkie Oh my! How could I not think of that?! Thanks alot for helping out!
B
48
S
20
G
6
Posts: 73
Reputation: 6,474

Post » Thu Oct 06, 2016 8:31 am

If I understand correctly the issue you mentioned at first, you want the bullet to be destroyed when it reaches a certain destination, right ?
The issue there is that the event doing so is a sub-event of the spawning of the bullet (which cannot logically work if the destination is on the other side of the screen).

What you want to do is separate that event, and also define a range instead of an absolute destination (most of the time, your bullet won't be going through the EXACT X and Y position).
Lastly you want the bullet to "remember " those coordinates.

So in the end you would have something like that instead (where destX and destY are instance variables of Sprite2 :
Image
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
294
S
117
G
96
Posts: 7,311
Reputation: 71,086

Post » Wed Oct 12, 2016 7:46 am

@Kyatric I had tried your method, and it works, thanks alot! By the way, I also found another way to do so by using the turret behaviour.

System Every Tick
- Player : Add Turret target sprite1

On any touch start
- System : Create object sprite1 on layer 0 at (Touch.X, Touch.Y)

On any touch end
- Sprite1 : Destroy
B
48
S
20
G
6
Posts: 73
Reputation: 6,474

Post » Wed Oct 12, 2016 2:33 pm

plinkie wrote:The simplest way to do this is on any touch create a different invisible object for detection at touch.x, touch.y and destroy sprite2 when overlapping the created object.


niiiiiiice! i like the way you think!
B
27
S
5
G
1
Posts: 33
Reputation: 1,447

Post » Wed Oct 12, 2016 2:50 pm

Bullet behavior has a setting for distance traveled, I believe. So:

Bullet, compare distance traveled, if > (start.x, start.y, finish.x, finish.y) then destroy bullet

Of course those are just placeholder terms. If the player doesn't move you can use player.x & player.y, but if he does move either add instance variables for startX & startY, or just create a 'distance' variable and on created set distance to distance(player.x, player.y, touch.x, touch.y).

This is a better solution than creating invisible sprites as doesn't create unnecessary sprites, doesn't' have to check for the collision, and doesn't have the chance of (somehow) not hitting the invisible sprite—or hitting an invisible sprite meant for a different bullet.
ImageImageImage
B
44
S
26
G
11
Posts: 517
Reputation: 10,500


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 24 guests