Help with my Swipe to throw functionality

Hey all,

I am having trouble “spawning” an object based on a swipe. My end goal is that the player swipes (hard or slow) and it spawns a throwing object that hits a target and they both explode. I came over from UDK and Unity engines so it’s a bit of a challenge converting over (though I am loving CoronaSDK!!). I was trying to search through some code and there is soo much sample code out there! Unfortunately there is not much in the “swipe to throw” type games. So far, thanks to another Corona user, I have come up with this to get a image to spawn but it does not go anywhere and I was wondering if anyone can help me with this?
The main code I have been tweaking is the swipe right in the check direction functions

local beginX   
local beginY   
local endX   
local endY   
   
local xDistance   
local yDistance  
   
local bDoingTouch  
local minSwipeDistance = 10  
local totalSwipeDistanceLeft  
local totalSwipeDistanceRight  
local totalSwipeDistanceUp  
local totalSwipeDistanceDown  
   
function checkSwipeDirection()  
 if bDoingTouch == true then  
 xDistance = math.abs(endX - beginX) -- math.abs will return the absolute, or non-negative value, of a given value.   
 yDistance = math.abs(endY - beginY)  
 if xDistance \> yDistance then  
 if beginX \> endX then  
 totalSwipeDistanceLeft = beginX - endX  
 if totalSwipeDistanceLeft \> minSwipeDistance then  
 print("Swiped Left")  
 end  
 else   
 totalSwipeDistanceRight = endX - beginX  
 if totalSwipeDistanceRight \> minSwipeDistance then  
 print("Swiped Right")  
 local star = display.newImage("star.jpg")  
 physics.addBody(star, {density = 1, friction = 0, bounce = 0})  
  
 end  
 end  
 else   
 if beginY \> endY then  
 totalSwipeDistanceUp = beginY - endY  
 if totalSwipeDistanceUp \> minSwipeDistance then  
 print("Swiped Up")  
 end  
 else   
 totalSwipeDistanceDown = endY - beginY  
 if totalSwipeDistanceDown \> minSwipeDistance then  
 print("Swiped Down")  
 end  
 end  
 end  
 end  
 end  
 function swipe(event)  
 if event.phase == "began" then  
 bDoingTouch = true  
 beginX = event.x  
 beginY = event.y  
 end  
 if event.phase == "ended" then  
 endX = event.x  
 endY = event.y  
 checkSwipeDirection();  
 bDoingTouch = false  
 end  
end  
   
Runtime:addEventListener("touch", swipe)  

Thanks! [import]uid: 183158 topic_id: 32914 reply_id: 332914[/import]

Hi Sammy,
Welcome to Corona! This code is a really good start. What you need to add is some kind of physical force or impulse upon the object after you create it (I see the star created in line 31 above). The force or impulse that you apply will need to be calculated from your swipe “data”, as in, it will depend on the angular direction of the swipe and also the power/speed of the swipe.

With this info, you can inspect the potential physics methods here:
http://docs.coronalabs.com/api/type/Body/index.html#methods

It’s going to be tricky to get the simulation to “feel right” (it usually is when simulating real-world physics in a game). But, some persistence will get you there before you know it.

Best regards,
Brent Sorrentino [import]uid: 9747 topic_id: 32914 reply_id: 130785[/import]

Hi Sammy,
Welcome to Corona! This code is a really good start. What you need to add is some kind of physical force or impulse upon the object after you create it (I see the star created in line 31 above). The force or impulse that you apply will need to be calculated from your swipe “data”, as in, it will depend on the angular direction of the swipe and also the power/speed of the swipe.

With this info, you can inspect the potential physics methods here:
http://docs.coronalabs.com/api/type/Body/index.html#methods

It’s going to be tricky to get the simulation to “feel right” (it usually is when simulating real-world physics in a game). But, some persistence will get you there before you know it.

Best regards,
Brent Sorrentino [import]uid: 9747 topic_id: 32914 reply_id: 130785[/import]