Drag touch to target enemy

Hello guys, I’ve been mulling away trying to find a solution to my problem.

I have a hero character that uses a touch on the hero object and a drag to use transition to in order to move to where the touch was released.

I have now introduced enemies into my game and I would like to add the ability for my hero to attack.

The goal is to touch my hero and drag the touch over an enemy and upon release my hero will transition to the enemy and start an auto attack. If touch is released on an area not occupied by an enemy I want the hero to just transition to the position as it does now.

I have been reading through the forums and have not really found an answer as to how to accomplish this so far.

Reading through the API I think it might be useful to use content bounds and track the enemies x & y min & max.

The only problem is that idk how to really track when touch is released within the bounds. Is it just as simple as having an if “released” and event.x > xMin etc… Then transition to enemy.x?

I’d appreciate any help

Hi Ethan,

We should see some code in order to help you further. In summary, it largely depends on if you’re setting “focus” on the hero or not. I know how to advise you in both cases, and if I see your code, it will be easier and faster. :slight_smile:

For posting code in the forums, remember to surround it with “lua” tags for clarity:

[lua] -- code [/lua]

Brent

sure, I’m pretty sure im not setting focus on my actor in the traditional way, but I may be doing some sort of work around, im not sure.

here is the movement function that I am using

function moveChar(event) if event.phase == "ended" and activeChar ~= nil then transition.cancel(activeChar) -- cancels any active movement if you make another transition.cancel("enemyMove") -- calculate distance to help move at constant speed local function distBetween(x1, y1, x2, y2) local xFactor = x2 - x1 local yFactor = y2 - y1 local dist = math.sqrt((xFactor\*xFactor) + (yFactor\*yFactor)) return dist end -- actual movement if event.phase == "ended" and activeChar ~= nil then if event.y \< 200 then event.y = 200 -- this is top boundry probably will change once I make universal for all apps end if event.y \> 700 then event.y = 700 -- this is bottom boundry probably will change once I make universal for all apps end transition.moveTo (activeChar, {x=event.x, y=event.y, time= distBetween(activeChar.x, activeChar.y, event.x, event.y)/activeChar.speed}) activeChar = nil end end end

i also have this set up as my own “set focus” I guess.

function charTouched\_Lily(event) if event.phase == "began" then activeChar = Lily print(Lily.health) --target = LilyS1 -- change enemy target for time being, will be changed later end end

when touch phase is “began” I change a variable called activeChar to the Hero I touched and you can see in the movement functions, when touch is release I set activeChar back to nil.

I guess what im looking to do is somewhere in the movement function I want to establish the ability to release the touch on an enemy and instead of just moving the Hero to the location, I want it to move to the enemy and start an auto attack.

OK great, this should actually be easier than if you used the Corona-enabled focus method (which as I said can be incredibly useful and necessary in some cases, but not always).

What I suggest is that, during the “ended” phase, you detect if the object upon which the touch ended is an enemy. If it is, do your character movement/auto-attack and set that enemy as the victim. :slight_smile:

Brent

detecting if the object upon which the touch ended is an enemy is the issue I was pondering. I’m not sure how I would do that. Do I have to track the enemy bounds (x & y min & max) and see if the touch release is within those bounds, or is there an easier way to detect if a touch is released over an object

Hi Ethan,

Well basically, if you don’t have “focus” (Corona’s focus method) on some object, whatever object the touch is released on will be the target of the “ended” phase, thus the enemy. Nice and simple. :slight_smile:

Brent

Thank you, I had a feeling it was a lot more simple than I was making it out to be.

Hi Ethan,

We should see some code in order to help you further. In summary, it largely depends on if you’re setting “focus” on the hero or not. I know how to advise you in both cases, and if I see your code, it will be easier and faster. :slight_smile:

For posting code in the forums, remember to surround it with “lua” tags for clarity:

[lua] -- code [/lua]

Brent

sure, I’m pretty sure im not setting focus on my actor in the traditional way, but I may be doing some sort of work around, im not sure.

here is the movement function that I am using

function moveChar(event) if event.phase == "ended" and activeChar ~= nil then transition.cancel(activeChar) -- cancels any active movement if you make another transition.cancel("enemyMove") -- calculate distance to help move at constant speed local function distBetween(x1, y1, x2, y2) local xFactor = x2 - x1 local yFactor = y2 - y1 local dist = math.sqrt((xFactor\*xFactor) + (yFactor\*yFactor)) return dist end -- actual movement if event.phase == "ended" and activeChar ~= nil then if event.y \< 200 then event.y = 200 -- this is top boundry probably will change once I make universal for all apps end if event.y \> 700 then event.y = 700 -- this is bottom boundry probably will change once I make universal for all apps end transition.moveTo (activeChar, {x=event.x, y=event.y, time= distBetween(activeChar.x, activeChar.y, event.x, event.y)/activeChar.speed}) activeChar = nil end end end

i also have this set up as my own “set focus” I guess.

function charTouched\_Lily(event) if event.phase == "began" then activeChar = Lily print(Lily.health) --target = LilyS1 -- change enemy target for time being, will be changed later end end

when touch phase is “began” I change a variable called activeChar to the Hero I touched and you can see in the movement functions, when touch is release I set activeChar back to nil.

I guess what im looking to do is somewhere in the movement function I want to establish the ability to release the touch on an enemy and instead of just moving the Hero to the location, I want it to move to the enemy and start an auto attack.

OK great, this should actually be easier than if you used the Corona-enabled focus method (which as I said can be incredibly useful and necessary in some cases, but not always).

What I suggest is that, during the “ended” phase, you detect if the object upon which the touch ended is an enemy. If it is, do your character movement/auto-attack and set that enemy as the victim. :slight_smile:

Brent

detecting if the object upon which the touch ended is an enemy is the issue I was pondering. I’m not sure how I would do that. Do I have to track the enemy bounds (x & y min & max) and see if the touch release is within those bounds, or is there an easier way to detect if a touch is released over an object

Hi Ethan,

Well basically, if you don’t have “focus” (Corona’s focus method) on some object, whatever object the touch is released on will be the target of the “ended” phase, thus the enemy. Nice and simple. :slight_smile:

Brent

Thank you, I had a feeling it was a lot more simple than I was making it out to be.