General question about click / drag

Also this is what I have set up which is supposed to drag the enemy

function dragEnemy(e) if(e.phase == 'began') then lastX = e.x - enemy.x print("DRAG") elseif(e.phase == 'moved') then enemy.x = e.x - lastX end end

A few questions:

  • What causes your enemy movement code to execute?  Is it being called by an enterFrame event

  • Where do you add the dragEnemy function as a touch listener to the enemy?

  • When you touch an enemy, do you see DRAG printed in the console?

  • Andrew

I

In regards to your questions

  • My enemy movement code is executed by a Timer - PerformWithDelay.

timerEnemy = timer.performWithDelay(2000, addEnemy, 0) 

and its being called from a GameLoop function which is called every second

  • The touch listener is: enemy:addEventListener(‘touch’, dragEnemy) and is called at the bottom of the code

  • And yes “DRAG” appears in the console, and enemy moves slightly forward then stops

* Sorry about the post above this, my computer went crazy and submitted that. 

OK, we’re getting there.  So a few more things:

  • In your dragEnemy function, you refer to a variable called enemy.  But where is that variable declared?  You probably want to be using e.target instead, which will refer to the object that’s being touched

  • In your addEnemy function, your first line is “enemy.name”.  That should generate an error unless enemy is a variable that was previously declared as a table or display object.  Are you previously declaring it somewhere?

  • You pasted a timer.performWithDelay for when enemies are added.  But that still doesn’t show what fires the code that causes the enemies to move

  • What causes the GameLoop function to fire?  Another repeating timer.performWithDelay()?

  • Andrew

  • I have ‘enemy’ declared as a global variable at the top of my code. I did this because Corona was telling me that global enemy has a nil value. I’ll remove that variable and add e.target instead inside the drag function.

  • I have… enemy = display.newImage(‘enemy.png’)… displayed up near the global variable incase thats what you meant. It probably should not be there. That and the …local enemy… are the only code I have elsewhere which connects with enemy itself. 

  • This fires the update function 

    Runtime:addEventListener(‘enterFrame’, update)

and the update function (which moves the enemy from right to left) is: 

 function update (event) if(enemies.numChildren ~= 0) then for i = 1, enemies.numChildren do if(enemies[i] ~= nil) then enemies[i].x = enemies[i].x - 3 enemies.speed = math.random(1, 6) if(enemies[i].x \< -150) then enemies:remove(enemies[i]) display.remove(enemies[i]) print("UFO destroyed") end end end end end

Hope that helps, also thanks for taking the time to help out. 

I added the e.target instead of enemy and it works slightly better than before but still does not drag as much as id hoped. Is it something wrong with the way the enemies are spawned?  

Have you taken a look and tried to implement the suggestions in my latest post?  (Sorry, for some reason it seems like that post didn’t go to the very bottom of the thread, but instead went one above, so you may not have seen it.)

  • Andrew

Do you mean the one before about the enemy variable and using e.target in the drag function? I have used that but its still not working properly. 

I dont think the latest post showed up. 

Oh yeah, you’re right, it didn’t go through for some reason.  Sorry about that.

What I said there was that, before we tackle the question of getting your enemy to be draggable, I think we need to address some bigger structural issues with your code:

  • Since you plan to have many enemies, you should haven’t a global ‘enemy’ variable.  It’s not clear what that variable would even refer to.  Instead, you should access your enemies through a table or display group, like you do in your update function
  • In your addEnemy function, you should have a line like “local enemy = display.newImage( … )”.  That way, you create a new enemy object each time addEnemy is called.  The addEnemy function is also where you should add the touch listener to the enemy you just created
  • I’m still not sure what the GameLoop function you mentioned does?

That code in the game loop (update) is basically supposed to move the enemies across screen and destroy when it exits the screen, or at least it should do that. Is there a better way that I should be moving them across the screen? 

However ill try out your soloution and get back to you.