Moving Pictures with a button

basically we have a button and we have a bunch of zombies spawning in every so seconds, we want it so that once the button is pressed we want all the zombies to transition to the left. here is the code we have that includes the zombies spawning and our button

if the buttons don’t work its because we didn’t include the photos so just use anything if u want to test it

local function spawnzombie()

    local zombie = display.newImageRect(“zombie.jpg”, 45, 45);

    

    zombie.x = 200;

    zombie.y = 200;

    function movezombie()

    transition.to(zombie,{time=7000, x=math.random(0,500), y=math.random(0,600), onComplete=movezombie});

end 

    physics.addBody(zombie, “dynamic”, {density = 0.1, bounce = 0.1, friction = .1, radius = 0});

    movezombie()

    --Adding touch event

    zombie:addEventListener(“touch”, zombie);

    zombie.collType = “none”

physics.addBody( zombie, { bounce=10.0, friction=1.0 } )

function onCollision(event)

–print(“collide!”)

happy:removeSelf()

end

Runtime:addEventListener(“collision”, onCollision)

end

local total_zombie = 100

tmr = timer.performWithDelay(2000, spawnzombie, total_zombie);

– BUTTON code 

local widget = require( “widget” )

local function handleButtonEvent( event )

local phase = event.phasr

if “ended” == phase then

print( “you pressed and released a button!” )

end

end

local myButton = widget.newButton

{

left = 800,

top = 400,

width = 100,

height = 100,

defaultFile = “button1.jpg”,

overFile = “smallbutton.png”,

label = “GRENADE”,

onEvent = handleButtonEvent,

}

There are several things going on.  First you don’t store a reference to each zombie, so you have to way of accessing them later to do things like move them, etc.  Next you are using transition.to() which is fine for static things, but for dynamic physics objects, it’s fighting with the physics engine.  You are better off setting a velocity on them rather than trying to transition them.  Look in the docs for setLinearVelocity.  If you need to change the direction, simply switch between giving them a positive velocity and a negative velocity.

Once you have references (perhaps store the zombie’s display.newImageRect()'s in a table), then you can simply iterate over the list of zombies and change their velocity as you see fit.

Rob

What’s the question or issue ?

Anyway, there is a typo @ :

local phase = event.phasr

when the button is pressed, i want the zombies to move all to one side. im a complete beginner at this so ya…

There are several things going on.  First you don’t store a reference to each zombie, so you have to way of accessing them later to do things like move them, etc.  Next you are using transition.to() which is fine for static things, but for dynamic physics objects, it’s fighting with the physics engine.  You are better off setting a velocity on them rather than trying to transition them.  Look in the docs for setLinearVelocity.  If you need to change the direction, simply switch between giving them a positive velocity and a negative velocity.

Once you have references (perhaps store the zombie’s display.newImageRect()'s in a table), then you can simply iterate over the list of zombies and change their velocity as you see fit.

Rob

What’s the question or issue ?

Anyway, there is a typo @ :

local phase = event.phasr

when the button is pressed, i want the zombies to move all to one side. im a complete beginner at this so ya…