I have developed a slingshot game, but when shooting a stong it not going smoothly. are there any example I can refer.i have followed Throw-a-Crow example. but I cannot use it to my scenario.i have included a screenshot. please help me
this is the code i used,
local min_y = 141
local max_y = 517
function init(self)
msg.post("#collisionobject", "disable")
self.initial_position = go.get_position()
self.initial_rotation = go.get_rotation()
self.release_rock = false;
self.flinging = false -- true if we are currently flinging
self.aiming = false -- true if we are currently aiming
self.panning = false -- true if we are currently panning around the level
self.rubber_length = 0
self.in_katapole = true;
self.iscollision = false
self.collisionAction = 'miss'
self.isSoundOn = true
msg.post(".", "acquire_input_focus")
end
function final(self)
msg.post("main:/go#main", "completly distroy rock", {id =
go.get_id(),iscollision=self.iscollision,collisionAction=self.collisionAction})
end
function update(self, dt)
-- Add update code here
-- Remove this function if not needed
if self.in_katapole then
local p = go.get_world_position("rockcontainer")
local xx = go.get_position()
if p.x < min_y then
p.x = min_y
elseif p.x > max_y then
p.x = max_y
end
xx.x = p.x;
go.set_position(xx)
end
--
-- do we have a bird in the air?
-- in this case we check if the bird is idle or still moving
--
if self.flinging then
end
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
if message_id == hash("distroy rock") then
go.delete(go.get_id())
particlefx.play("#explosion")
elseif message_id == hash("collision_response") then
pprint("collision_response",message.other_group)
self.iscollision = true
if message.other_group==hash("head") then
self.collisionAction = 'head';
else
self.collisionAction = 'body';
end
go.delete(go.get_id())
particlefx.play("#explosion")
msg.post("main:/monkey", "hit monkey")
elseif message_id == hash("enable sounds") then
self.isSoundOn = true
print("enable sounds")
elseif message_id == hash("disable sounds") then
self.isSoundOn = false
print("disable sounds")
elseif message_id == hash("enable music") then
self.ismusicon = true;
elseif message_id == hash("disable music") then
self.ismusicon = false;
end
end
function on_input(self, action_id, action)
--pprint(action)
local action_position = vmath.vector3(action.x, action.y, 0)
--print("action_position",action_position)
if action.pressed then
-- did we click on the bird?
-- if yes, start aiming
-- if no, start panning
--
if vmath.length(action_position - go.get_world_position()) < 100 then
self.aiming = true
self.pressed_position = action_position
msg.post("#sprite","disable")
msg.post("main:/go#main","hide hand move animation")
print("click on rock")
if self.isSoundOn then
msg.post("main:/sounds#catapultpulld", "play_sound")
end
else
print("not click on rock")
self.panning = true
self.pressed_position = action_position
end
elseif action.released then
print("released")
if self.aiming then
local leng = self.rubber_length;
for i = self.rubber_length, 0,-1 do
-- rubber animation
local url = msg.url("main:/rubber#sprite")
local prefix = '';
local dis = i;
if dis < 10 then
prefix = '00'
elseif dis < 100 then
prefix = '0'
else
prefix = ''
end
if dis >= 0 and dis < 151 then
sprite.play_flipbook(url, 'Armature_animtion0_'..prefix..dis);
if self.isSoundOn then
msg.post("main:/sounds#rockrelase", "play_sound")
end
end
end
self.rubber_length = 0;
local direction = self.initial_position - go.get_position()
pprint("direction",direction)
go.animate(".", "scale", go.PLAYBACK_LOOP_FORWARD, 0.1, go.EASING_OUTSINE, 1)
msg.post("#collisionobject", "enable")
local newDir = vmath.vector3(0, direction.y, 0)
msg.post("#collisionobject", "apply_force", { force = newDir * 600 * go.get("#collisionobject", "mass"), position = go.get_world_position() })
msg.post(".", "release_input_focus")
self.flinging = true
self.aiming = false
self.idle_frames = 0
self.pressed_position = nil
local url = msg.url("main:/rubber#sprite")
sprite.play_flipbook(url, 'without rock for animation 4');
print("Rubber return to init position")
--
timer.delay(0.1, false, function()
msg.post("#sprite","enable")
end)
timer.delay(0.8, false, function()
msg.post(".","distroy rock")
end)
else
print("self.panning = false")
self.panning = false
end
elseif self.aiming then
--print("aiming")
--
-- calculate the distance we've moved from the position where we started
-- dragging the rock
-- limit this distance to below a threshold value
--
local kat_location = go.get_position("katapolaya")
local rock_position = go.get_position();
--print(self.pressed_position,action_position,kat_location,rock_position)
-- calculate distance
local dy = self.pressed_position.y - action_position.y;
local distance = dy
local max_distance = 150
if distance > max_distance then
dy = max_distance
distance = max_distance
end
-- rubber animation
local url = msg.url("main:/rubber#sprite")
local prefix = '';
local dis = round(distance);
if dis < 10 then
prefix = '00'
elseif dis < 100 then
prefix = '0'
else
prefix = ''
end
if dis > 0 and dis < 151 then
--print("dis",dis)
sprite.play_flipbook(url, 'Armature_animtion0_'..prefix..dis);
self.rubber_length = dis
go.set_position(vmath.vector3(0, self.initial_position.y - dy, 1))
end
end
end
function round(n)
return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end
function left_right()
go.delete()
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end