New Design of Angry Birds Catapult based on simplePool example

Hi, I developed with the help of the simplePool example an angryBird catapult that I want to share with you guys if it don’t bother anyone. It’s pretty different from the Hot Cross Bunny example from the code exchange section, but I think it could be useful to have more than one different version of the thing… This code is detailing the object that you can after duplicate in instances in your program. Enjoy and give me your toughs !

  
module(..., package.seeall)  
  
local projectile = require("projectile")  
local physics = require("physics")  
local catapult = require("catapult")  
--physics.setDrawMode( "hybrid" )  
physics.start()  
  
physics.setScale(60) --valeur physique pour les petits objets  
physics.setGravity(0, 0) --Vue que la vue est de haut, il n'y a pas de vecteur de gravité  
  
function newCatapult( \_x, \_y, teamColor )  
  
--Groupe visible  
local slingshot\_container = display.newGroup();  
  
local poteau\_haut = display.newImage( "slingshot\_pole\_unique.png")  
poteau\_haut.x = \_x; poteau\_haut.y = \_y; poteau\_haut:scale(2,2)  
slingshot\_container:insert(poteau\_haut)  
  
local poteau\_bas = display.newImage("slingshot\_pole\_unique.png")  
poteau\_bas.x = \_x; poteau\_bas.y = \_y + 200; poteau\_bas:scale(2,2)  
slingshot\_container:insert(poteau\_bas)  
  
local elastique = display.newLine(poteau\_haut.x, poteau\_haut.y, poteau\_bas.x, poteau\_bas.y)  
-- Set the elastic band's visual attributes  
elastique:setColor(214,184,130);  
elastique.width = 6;  
slingshot\_container:insert(elastique)  
  
--On cree notre boule de neige  
local ball = projectile.newProjectile()  
ball.x = \_x; ball.y = \_y+100  
slingshot\_container:insert(ball)  
  
--On crée la cible vituelle autour de la balle  
local target = display.newImage( teamColor)  
target.x = ball.x; target.y = ball.y; target.alpha = 0  
slingshot\_container:insert(target)  
  
local function dragCatapult( event )  
 local t = event.target  
  
 local phase = event.phase  
 if "began" == phase then  
 display.getCurrentStage():setFocus(t)  
 t.isFocus = true  
  
 --On stoppe tout mouvement de la balle  
 t:setLinearVelocity(0,0)  
 t.angularVelocity = 0  
 --cible par rapport à la balle  
 target.x = t.x  
 target.y = t.y  
 --position initiale au toucher   
 t.x0 = event.x - t.x  
 t.y0 = event.y - t.y  
  
 --petite fonction gérant l'animation de la cible  
 startRotation = function()  
 target.rotation = target.rotation + 4  
 end  
 Runtime:addEventListener("enterFrame", startRotation)  
  
 --affichage de la cible au toucher   
 local showTarget = transition.to(target, {alpha = 0.4, time = 200, yScale = 0.4, xScale = 0.4})  
 --Ne pas oublier de spécifier que la ligne est null  
 myLine = nil  
 --lignes de slingshot  
 line\_up = nil  
 line\_down = nil  
  
elseif t.isFocus then  
  
 --On cache l'elastique de départ  
 elastique.isVisible = false  
  
 if "moved" == phase then  
 --Si la ligne existe, efface-la  
 if (myLine) then  
 myLine.parent:remove(myLine)  
 line\_up.parent:remove(line\_up)  
 line\_down.parent:remove(line\_down)   
 end  
  
 -- la balle suit le toucher...  
 t.x = event.x - t.x0  
 t.y = event.y - t.y0  
  
 --Set the elastic attached to the touch  
 line\_up = display.newLine(t.x, t.y, poteau\_haut.x, poteau\_haut.y)  
 line\_down = display.newLine(t.x, t.y, poteau\_bas.x, poteau\_bas.y)  
 -- Set the elastic band's visual attributes  
 line\_up:setColor(214,184,130);  
 line\_up.width = 4;  
  
 line\_down:setColor(243,207,134);  
 line\_down.width = 4;  
 --Set the target line visual attributes  
 myLine = display.newLine(t.x, t.y, target.x, target.y)  
 myLine:setColor(0,0,0,50)  
 myLine.width = 8  
  
elseif "ended" == phase or "cancelled" == phase then  
 -- relâche le focus  
 display.getCurrentStage():setFocus( nil )   
 t.isFocus = false  
 --Fonction arrêtant la cible de tourner  
 local stopRotation = function()  
 Runtime:removeEventListener("enterFrame", startRotation)  
end  
--Utilisation de la fonction stopRotation après que la cible est disparu  
local hideTarget = transition.to(target, {alpha = 0, time = 200, yScale = 1.0, xScale = 1.0, onComplete=stopRotation})  
  
--On fait réaparaître l'élastique de départ  
elastique.isVisible = true  
  
--On efface toutes les lignes  
if (myLine) then  
 myLine.parent:remove(myLine)  
 line\_up.parent:remove(line\_up)  
 line\_down.parent:remove(line\_down)  
 line\_up = nil  
 line\_down = nil  
end  
--Envoie de la balle  
--On applique la force selon la distance entre la balle et la cible \* -600 vue que c'est un lancer dans la direction contraire...  
t:applyForce((t.x - target.x)\*-600, (t.y - target.y)\* -600, t.x, t.y)  
  
end  
end  
  
return true  
  
end  
  
local function toucheLimite( event )  
 if ball.x \> display.contentWidth+200 or ball.x \< -200 or ball.y \< -200 or ball.y \> display.contentHeight+200 then  
 ball:setLinearVelocity(0,0)  
 ball.angularVelocity = 0  
 ball.x = \_x  
 ball.y = \_y+100  
  
 end   
end  
  
Runtime:addEventListener("enterFrame", toucheLimite)  
ball:addEventListener("touch", dragCatapult )  
  
return slingshot\_container  
  
end  
  

and this is the code for the projectile object from the Hot Cross Bunnies example:

--   
-- Abstract: Part of a tutorial documenting how to put together an 'Angry Birds'/'Hot Cross Bunnies' elastic catapult in CoronaSDK.  
-- Visit: http://www.fixdit.com regarding support and more information on this tutorial.  
-- Hot Cross Bunnies is now available in the iTunes App Store: http://itunes.apple.com/app/hot-cross-bunnies/id432734772?mt=8  
--  
-- Version: 1.0  
--   
-- Copyright (C) 2011 FIXDIT Ltd. All Rights Reserved.  
--  
-- Permission is hereby granted, free of charge, to any person obtaining a copy of   
-- this software and associated documentation files (the "Software"), to deal in the   
-- Software without restriction, including without limitation the rights to use, copy,   
-- modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,   
-- and to permit persons to whom the Software is furnished to do so, subject to the   
-- following conditions:  
--   
-- The above copyright notice and this permission notice shall be included in all copies   
-- or substantial portions of the Software.  
--   
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR   
-- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE   
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR   
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER   
-- DEALINGS IN THE SOFTWARE.  
  
-- main.lua (Part of a tutorial documenting how to put together an 'Angry Birds'/'Hot Cross Bunnies' elastic catapult in CoronaSDK.)  
  
module(..., package.seeall)  
  
-- Pass state reference  
state = {};  
-- Bullet starts off in-active  
ready = false;  
-- Pass audio references  
shot = {};  
band\_stretch = {};  
  
function newProjectile()  
  
 -- Import easing plugin  
 local easingx = require("easing");  
  
 -- Bullet properties  
 local snowball\_bullet = {  
 name = "snowball",  
 type = "bullet",  
 density= 0.8,  
 friction= 0.2,  
 bounce= 0.5,  
 size = 15,  
 rotation = 0  
 }  
  
 -- Init bullet  
 local bullet = display.newImage("".. snowball\_bullet.name .. ".png");  
 -- Place bullet  
 bullet.x = 0; bullet.y = 0;  
 -- Set up physical properties   
 physics.addBody(bullet, "dynamic", {density=snowball\_bullet.density, friction=snowball\_bullet.friction, bounce=snowball\_bullet.bounce, radius=snowball\_bullet.size});  
  
 bullet.linearDamping = 0.3;  
 bullet.angularDamping = 0.8;  
 bullet.isBullet = true;  
 -- Transition the bullet into position each time it's spawned   
 transition.to(bullet, {time=600, y= \_y, transition = easingx.easeOutElastic});  
  
 return bullet;  
  
end  
  

I don’t know if I’m allowed to post code like this but if it bothers the forum, I’m willing to post it in the code exchange section of the site…

ray [import]uid: 20617 topic_id: 11802 reply_id: 311802[/import]

Hey Ray :slight_smile:

This wont bother anyone! Learning, experimenting and sharing is a hugely important part of a success community - good for you!

I haven’t got time to test this out myself just yet but I would encourage you to submit it to the code exchange, certainly - more code there means people have more to learn from, then experiment and share themselves.

Peach :slight_smile: [import]uid: 52491 topic_id: 11802 reply_id: 43020[/import]

Here is the page where my source code is if anyone have problem to run it with the code below…

http://developer.anscamobile.com/code/angry-bird-catapult-simple-pool-example [import]uid: 20617 topic_id: 11802 reply_id: 43035[/import]