Set parent

Get help using Construct 2

Post » Thu Dec 29, 2011 4:12 pm

I'd like to do something like this:
There's many soldiers on the map. They all moves randomly. All of them has its own crosshair (aim). If soldier sees another soldier in his crosshair he shoots.

There's my pseudo-code for this:
events:
- Start of level
- for every soldier
Action:
- create crosshair
- pin crosshair to soldier

event:
- crosshair collides with soldier
action:
- soldier fire

But wrong soldier is firing (not shooter-soldier but target-soldier is shooting).

I think it should be done like this:
events:
- Start of level
- for every soldier
Action:
- create crosshair
- pin crosshair to soldier
- for that crosshair set this soldier as parent

event:
- crosshair collides with soldier
action:
- parent soldier fire


Is there any action similar to parenting?tecbug2011-12-29 16:15:11
B
53
S
14
G
10
Posts: 282
Reputation: 7,578

Post » Thu Dec 29, 2011 4:25 pm

Im not quite sure what you are trying to explain, Please provide your capx, it gives us more of a chance to help you.

Are your soldiers separate sprites or just different instances of one?

You might have to assign a separate id to each soldier, in the sprites properties panel,

Try explain the game more,

Are you setting a cursor to the mouses position, and if the cursor collided with a soldier you want it to fire?
B
29
S
12
G
7
Posts: 740
Reputation: 7,839

Post » Thu Dec 29, 2011 4:55 pm

I think I understand what you want to do.

You want basically pick the soldier that possess the crosshair

You can do that quite simply like that :

create two instance variable for the crosshair object:
- soldierUID type number default 0
- firing type bolean default false
[code]Global variable crossDist = 100 // Distance between soldier and crosshair
System: On start of layout
   -> destroy crosshair
   System: Foreach Soldier
      -> System: create crosshair on layer 0 at cos(soldier.angle)*crossDist,sin(sodlier.angle)*crossDist
      -> crosshair: pin to Soldier
      -> crosshair: set sodlierUID to Soldier.UID
System: Every tick
   -> crosshair: set firing to false
crosshair: is overlapping Soldier
   -> crosshair: set firing to true
crosshair: is firing
soldier: pick instance with UID crosshair.soldierUID
      -> Sodlier: spawn Bullet
      -> Bullet: set angle to Soldier.angle[/code]Yann2011-12-29 16:56:55
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Fri Dec 30, 2011 6:47 am

Be sure also to read (again) how events work this could help you clean out your code.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Fri Dec 30, 2011 2:05 pm

Yann, thanks. It works perfectly.
Kyatric, I sure will.
B
53
S
14
G
10
Posts: 282
Reputation: 7,578


Return to How do I....?

Who is online

Users browsing this forum: Megavolt, mnashwan and 20 guests