attach weapons to sprites in runtime?

Hello all,

Is there a way in corona to add weapons in runtime to the sprites? to a specific pixel let’s say to player’s hands so when his hands rotate the weapon also rotates?

Thanks!

Here’s a tutorial regarding dynamic sprites and the best way to maximize performance:

http://www.coronalabs.com/blog/2012/10/09/dynamically-optimized-sprite-sheets/

I couldn’t get it :frowning:

What couldn’t you get? 

My character can pickup and use lots of different weapons. So do i have to create different animation for each weapon? like…

Walking animation

Walking with pistol animation

Walking with machingun animation

Walking with an baseball bat animation

Or i can attach each weapon to the player’s hand during runtime?

You can do the latter. It’s as easy as creating the player sprite and the weapon sprite, and setting a Runtime listener to do the following:

local function animInit() weaponSprite1.x = playerSprite.x+1 weaponSprite1.y = playerSprite.y+15 end Runtime:addEventListener("enterFrame", animInit)

If you’re using physics, you could also consider attaching the weapon to the player using a weld joint, then releasing (breaking) the weld joint when you want to “drop” the weapon.

Brent

Hmmmm now i think i understand the logic behind it…

I am using physics…That is a great Idea!

But i am already 70% finished animating my character with each weapon…Which technique do you think will be more memory friendly? the methods you guys described or my own method of animating my character with each weapon and use one sprite instead of 2???

Thanks again!

I’ve never used welded joints so I’ll differ to Brent on this one.

There is a problem now…

How do i keep up the rotation of the gun with the rotation of the hand???

Thanks again!

Hi @coolromin,

Weld joints are easy: just weld the two objects together at a point in world coordinates. This would likely be the player’s hand or arm, in your situation.

[lua]

physics.newJoint( “weld”, object1, object2, anchorX, anchorY )

[/lua]

http://docs.coronalabs.com/api/library/physics/newJoint.html

This will also handle the rotation… the weapon essentially becomes a part of the player’s body, within reasonable force limits (meaning that, weld joints can occasionally “flex” under extreme applications of force, but typically they are rigid).

Brent

If i decide to go for the classic way (animate my character with each weapon and use one sprite instead of 2 sprites), will it take a lot of memory comparing to the methods you guys described???

I don’t know the complexity of your animations, but it will take up a bit more memory, both texture and general.

Here’s a tutorial regarding dynamic sprites and the best way to maximize performance:

http://www.coronalabs.com/blog/2012/10/09/dynamically-optimized-sprite-sheets/

I couldn’t get it :frowning:

What couldn’t you get? 

My character can pickup and use lots of different weapons. So do i have to create different animation for each weapon? like…

Walking animation

Walking with pistol animation

Walking with machingun animation

Walking with an baseball bat animation

Or i can attach each weapon to the player’s hand during runtime?

You can do the latter. It’s as easy as creating the player sprite and the weapon sprite, and setting a Runtime listener to do the following:

local function animInit() weaponSprite1.x = playerSprite.x+1 weaponSprite1.y = playerSprite.y+15 end Runtime:addEventListener("enterFrame", animInit)

If you’re using physics, you could also consider attaching the weapon to the player using a weld joint, then releasing (breaking) the weld joint when you want to “drop” the weapon.

Brent

Hmmmm now i think i understand the logic behind it…

I am using physics…That is a great Idea!

But i am already 70% finished animating my character with each weapon…Which technique do you think will be more memory friendly? the methods you guys described or my own method of animating my character with each weapon and use one sprite instead of 2???

Thanks again!