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?