hello. i have a problem.
this is my first owen attempt to code something, before i had done some tutorials, but i´m still a noob -.-
okey, ther is my current problem:
i have a image, called “soldat”, he walks up and down on the map (bg) and rotate itself to the tappet position:
physics.addBody(soldat) soldat.linearDamping = 1 soldat.enterFrame = function(self,event) print(self.x,self.tx) --This condition will stop the circle on touch coordinates --You can change the area value, this will detect if the circles's x and y is between the circles's tx and ty --If area is 0, it may not stop the circle, area = 5 is safe, change if you want to local area = 20 if self.x \<= self.tx + area and self.x \>= self.tx - area and self.y \<= self.ty + area and self.y \>= self.ty - area then soldat:setLinearVelocity(0,0) --set velocity to (0,0) to fully stop the circle end end --Add event listener for the monitoring the coordinates of the circle Runtime:addEventListener("enterFrame",soldat) Runtime:addEventListener("touch",function(event) if event.phase == "ended" or event.phase == "began" or event.phase == "moved" then local x, y = event.x, event.y local tx, ty = x-soldat.x, y-soldat.y local speedMultiplier = 2 --this will change the speed of the circle, 0 value will stop the circle angle = math.deg(math.atan2((event.y-soldat.y),(event.x-soldat.x))) soldat.rotation = angle + 90 --sets the future destination of the circle -- black.maskX = soldat.tx --black.maskY = soldat.ty soldat.tx = x soldat.ty = y delay = 1 soldat:setLinearVelocity(tx\*delay,ty\*delay) --this will set the velocity of the circle towards the computed touch coordinates on a straight path. end end
the problem there is, i have no force how he walks, i copied this part from somwhere else and modified it.
he “walks” like a car, not like an humen.
secound problem, i have a mask and this mask shoud follow the “soldat”, but the mask is doing what it wants,
one time, it moves only by tapping, but the “soldat” runs away, one time, the mask is moving correktly, but the “soldat” stays still.
I have no more ideas, please help me.
the complete Code::
display.setStatusBar(display.HiddenStatusBar) \_H = display.contentHeight; \_W = display.contentWidth; local physics = require("physics") physics.start() --physics.setDrawMode("hybrid") physics.setGravity(0, 0) system.activate("multitouch") local soldatleben = 1 local soldatMovex = 0 local soldatMovey = 0 local soldatSpeed = 3 local Rand = math.random local bg = display.newImage("bg.png") bg.anchorY = 0 bg.anchorX = 0 local leftbush = display.newImage("leftbush.png") leftbush.anchorY = 0 leftbush.anchorX = 0 leftbush.y = -200 leftbush.x = -65 local rightbush = display.newImage("rightbush.png") rightbush.anchorY = 0 rightbush.anchorX = 0 rightbush.x = 935 local topbush = display.newImage("topbush.png") topbush.anchorY = 0 topbush.anchorX = 0 topbush.y = -60 local botbush = display.newImage("botbush.png") botbush.anchorY = 0 botbush.anchorX = 0 botbush.y = 1795 local physics = require("physics") local speed = 50 physics.start() physics.setGravity(0,0) local black = display.newRect( 0, 0, 2000, 3020 ) black:setFillColor( 0 ) black.anchorY = 0.5 black.anchorX = 0.5 local soldat = display.newImage("soldat.png") soldat.x = \_W/2 soldat.y = \_H/2 soldat.tx = 0 soldat.ty = 0 maske = graphics.newMask( "alpha1.png") black:setMask( maske ) maskengruppe = display.newGroup() maskengruppe:insert( black, soldat ) maskengruppe.x = soldat.x maskengruppe.y = soldat.y maskengruppe.tx = 0 maskengruppe.ty = 0 --[[physics.addBody(soldat) soldat.linearDamping = 1 soldat.enterFrame = function(self,event) print(self.x,self.tx) --This condition will stop the circle on touch coordinates --You can change the area value, this will detect if the circles's x and y is between the circles's tx and ty --If area is 0, it may not stop the circle, area = 5 is safe, change if you want to local area = 20 if self.x \<= self.tx + area and self.x \>= self.tx - area and self.y \<= self.ty + area and self.y \>= self.ty - area then soldat:setLinearVelocity(0,0) --set velocity to (0,0) to fully stop the circle end end --Add event listener for the monitoring the coordinates of the circle Runtime:addEventListener("enterFrame",soldat) Runtime:addEventListener("touch",function(event) if event.phase == "ended" or event.phase == "began" or event.phase == "moved" then local x, y = event.x, event.y local tx, ty = x-soldat.x, y-soldat.y local speedMultiplier = 2 --this will change the speed of the circle, 0 value will stop the circle angle = math.deg(math.atan2((event.y-soldat.y),(event.x-soldat.x))) soldat.rotation = angle + 90 --sets the future destination of the circle -- black.maskX = soldat.tx --black.maskY = soldat.ty soldat.tx = x soldat.ty = y delay = 1 soldat:setLinearVelocity(tx\*delay,ty\*delay) --this will set the velocity of the circle towards the computed touch coordinates on a straight path. end end]]-- physics.addBody(maskengruppe) maskengruppe.linearDamping = 1 maskengruppe.enterFrame = function(self,event) print(self.x,self.tx) --This condition will stop the circle on touch coordinates --You can change the area value, this will detect if the circles's x and y is between the circles's tx and ty --If area is 0, it may not stop the circle, area = 5 is safe, change if you want to local area = 20 if self.x \<= self.tx + area and self.x \>= self.tx - area and self.y \<= self.ty + area and self.y \>= self.ty - area then maskengruppe:setLinearVelocity(0,0) --set velocity to (0,0) to fully stop the circle end end --Add event listener for the monitoring the coordinates of the circle Runtime:addEventListener("enterFrame",maskengruppe) Runtime:addEventListener("touch",function(event) if event.phase == "ended" or event.phase == "began" or event.phase == "moved" then local x, y = event.x, event.y local tx, ty = x-maskengruppe.x, y-maskengruppe.y local speedMultiplier = 2 --this will change the speed of the circle, 0 value will stop the circle --sets the future destination of the circle -- black.maskX = soldat.tx --black.maskY = soldat.ty maskengruppe.tx = x maskengruppe.ty = y delay = 1 maskengruppe:setLinearVelocity(tx\*delay,ty\*delay) --this will set the velocity of the circle towards the computed touch coordinates on a straight path. end end)
thank you guys