Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to pin child objects to parent object?
Started by stalxerhd Sep 19 2019 01:27 AM

3 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

stalxerhd

[GLOBAL: userInfoPane.html]
stalxerhd
  • Observer

  • 11 posts
  • Corona SDK

I need to pin objects. Speaking in terms of a "Star Destroyer" teaching game, I added a boss (which looks like a player starship) in front of a player ship.
 
Now I want to add a shield and armor for these ships, which made as two circles with blue and grey stroke, just displayed objects, no physics body. They displayed closely, circles radius are not big so I just don't care that laser shots will overlap them for a bit of a moment. 
 
The idea is when the collision happens, it removes some amount of a shield/armor HP, if one of that HP==0 I make display.remove and later the damage go to the ship HP.
 
Boss and a player ships are dynamic objects which constantly moves on X- and Y-axis. For boss ship trajectory declared randomly every 3 seconds with applyForce(), the player ship is moving via
function tapPlayerShip( event )

Important: I don't move ships to specific coordinates, instead I have unseen rectangles at the upper half of the screen for boss ship and in the lower half of the screen for the player ship, so they freely dangle there and shooting each other.

 

But how I could move the shield/armor circles? I went unoptimized way - grab the coordinates of the ship and set them to circles with 

timer.performWithDelay( 10, function () 
  CircleBossShield.x = BossShip.x CircleBossShield.y = BossShip.y 
  CircleBossArmour.x = BossShip.x CircleBossArmour.y = BossShip.y
end, 0 )

But the timer should work all the time, from 30-40 ms I already could see that shield and armour move with spurts. I also think that this method is costly for CPU/GPU.

 
The other way is to make armor like a gradient stroke of a ship, but I still need to move the shield (I could make only one stroke). So I spent enough time searching at the forums and at the API, but don't find a better way.
 
So the first question is about a more optimized way to pin objects, or maybe a Corona command to do it.
 
I also see the topic with "make a group and move it" advice.
 
So I make a massive:
local PlayerShipGroup ={ 
display.newImage(mainGroup,  "PlayerShip.jpg", 100, 100), 
display.newCircle( mainGroup, PSx, PSy, 55), 
display.newCircle( mainGroup, PSx, PSy, 60)}

Add to PlayerShipGroup[1] dynamic physics body and other settings, simply configure 2 and 3 items with setFillColor, strokeWidth, setStrokeColor, make a group

local myGroup = display.newGroup()
 myGroup:insert (PlayerShipGroup[1])
 myGroup:insert (PlayerShipGroup[2])
 myGroup:insert (PlayerShipGroup[3])

and try to move via 

local function tapGroup( event )
{
myGroup:setLinearVelocity( 0, 0) -- Stop current movement
myGroup:applyForce() -- Move to new position
}

but it doesn't work "attempt to call method 'setLinearVelocity' (a nil value)", as i understand, no physics body - no tapGroup, because I still can move it if set directly PlayerShipGroup[1]:setLinearVelocity( 0, 0) or PlayerShipGroup[1]:applyForce()

 
So the second question is maybe I do something wrong with groups?

Edited by stalxerhd, 19 September 2019 - 02:09 AM.


[TOPIC: post.html]
#2

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 596 posts
  • Corona SDK

Use a Runtime listener to "pin" your display objects together

local function pinObjects(  )
    CircleBossShield.x = BossShip.x ; CircleBossShield.y = BossShip.y 
    CircleBossArmour.x = BossShip.x ; CircleBossArmour.y = BossShip.y
end
Runtime:addEventListener( 'enterFrame', pinObjects )

 

This will move your display objects every frame.

 

 

For the second question, you can't set the linear velocity of the display group because it isn't a physics object.  The forces you are trying to apply will only work on physics objects.

 

*When moving objects and grouped objects that reference one another you should be familiar with contentToLocal( )

and also group programming in general

 



[TOPIC: post.html]
#3

stalxerhd

[GLOBAL: userInfoPane.html]
stalxerhd
  • Observer

  • 11 posts
  • Corona SDK

sporkfin, thanks!

 

How costly adding Runtime:addEventListener will be for CPU/GPU?



[TOPIC: post.html]
#4

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 596 posts
  • Corona SDK

It's not as costly as you think.  5 years ago it was more of a consideration but todays mobile device models can handle a lot of bandwidth and demand.  

 

Check out this sample

https://www.youtube.com/watch?v=zz6t9Xj94Yo

 

I use several Runtime listeners to make literally thousands of positional, relational, distance and lighting calculation every frame.




[topic_controls]
[/topic_controls]