best way to make a spawned actor follow the actor that spawned it?

StormyStudioStormyStudio United KingdomMember Posts: 3,989
edited November -1 in Working with GS (Mac)
Good evening...

Ok, I've tried the obvious ideas, and think I know a long way to achieve what I want but thought I'd check in case it's staring me in the face.

I have a block that the user can rotate using multitouch...works great.

I want it so when the block is pressed, a circle with arrows is spawned round the block, so the user knows it can be rotated.

I have it so the arrow is spawned, but it does not follow the block which can be moved with touch.

How can I set it up, so the spawned objects position follows that of the block, not just spawn at its centre, but stay with it.

I've tried using the 'position x and y' in the spawn attribute, but that only makes the spawned actor appear in the right place when spawned.

I have a few of these blocks which rotate, so can't simply put a rule in the circle prototype to constrain to the moveable block, as there are different moveable blocks.

My one way I guess which would work, is to make 2 'game attributes' that keeps the x and y of the moveable object selected. Which is reset each time there is no 'touch' active.

Then tell the circle prototype to match these values....

This should work, but sometimes in the game it is handy to be able to select more than one moveable object, so this may confuse GameSalad.....

Also, I can't simply have the circle follow the touch x and y, as i have it set up so that when you touch and move a block, the block does not move its centre to where the touch is. (it makes the game nicer to play)

So before I go ahead and set that up has anyone got any other work around?

Edit: Just tried my idea above, it works, but involves using 4 constrain behaviours, which are very processor heavy, Also the circle (which is set to non-moveable) slowly falls down, whilst visible. ?

Comments

  • firemaplegamesfiremaplegames Member Posts: 3,211
    yes, what you said.

    you need to keep track of the block's position with global atributes: blockX and blockY
    In the block actor, constrain those global attributes to self.PositionX and self.PositionY

    In the newly spawned actors, they need to constrain their position to the global blockX and blockY
  • ORBZORBZ Member Posts: 1,304
    elevate the block's x,y to game attributes.

    then inside your arrow define two variables:
    Block Offset Angle (Angle)
    Block Distance (Real)

    when the arrow is spawned have it use Change Attribute to initialize the Block Offset Angle and the Block Distance Angle.

    BlockOffsetAngle = vectorToAngle(block.x - self.position.x, block.y - self.position.y)
    BlockDistance = magnitude(block.x - self.position.x, block.y - self.position.y)

    then use some trig to constrain the spawned object to the block:

    self.position.x = cos(BlockOffsetAngle) * BlockDistance
    self.position.y = sin(BlockOffsetAngle) * BlockDistance

    optionally constrain the rotation if you want it to rotate with it:
    self.rotation = block.rotation

    btw, this may not work :) i haven't tested it... but the theory is sound, you may have to tweak the formulas a little bit.
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    Firemaple thanks...tried and it works, I do wonder whether it can be improved, also for somereason the circle still falls even though it is set up as you described, and it is set to not moveable? (falls slowly, like 1 pixel every 2 seconds, but keeps going...)

    @ Orbz: (oh started watching Khan Academy on youtube :-)....

    Started doing the way you've said, just checking where you say to use trig to constrain....

    do you mean to use a constrain attribute at that point?
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    @Orbz,

    have tried it, but the circle no longer appears on the screen, I'm assuming it has spawned somewhere of the canvas? tried using 'Change Attribute' and 'Constrain', where you mention the sin and cos parts...

    so back to the normal, constrain x and y of block to game.block X and Y, and then constrain x and y of circle to game.block X and Y. It works but for some reason the circle falls south, no gravity affecting it?
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    figured out why it was falling, the code which makes the object follow the touch position without moving the centre of the object to the touch, was happening after the code telling the game attrivute what the x and y were... getting a bit too rule heavy....
Sign In or Register to comment.