Skip to content

Commit

Permalink
Proposed example structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ar2rsawseen committed Jul 3, 2013
1 parent 0ee78d0 commit f089d9c
Show file tree
Hide file tree
Showing 30 changed files with 835 additions and 0 deletions.
Binary file added .tmp/KitchenSink.md5
Binary file not shown.
42 changes: 42 additions & 0 deletions KitchenSink.gproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<project>
<properties autorotation="0" encrypt="0" fps="60" scaleMode="3" mouseToTouch="1" iosDevice="0" packageName="com.yourdomain.yourapp" logicalHeight="480" logicalWidth="320" mouseTouchOrder="0" retinaDisplay="0" touchToMouse="1" assetsOnly="0" orientation="0" architecture="0">
<imageScales/>
</properties>
<folder name="examples">
<folder name="Audio_Example">
<file source="examples/Audio_Example/1-up.png"/>
<file source="examples/Audio_Example/1-down.png"/>
<file source="examples/Audio_Example/2-up.png"/>
<file source="examples/Audio_Example/2-down.png"/>
<file source="examples/Audio_Example/3-up.png"/>
<file source="examples/Audio_Example/3-down.png"/>
<file source="examples/Audio_Example/1.wav"/>
<file source="examples/Audio_Example/2.wav"/>
<file source="examples/Audio_Example/3.wav"/>
<file source="examples/Audio_Example/soundbutton.lua"/>
<file source="examples/Audio_Example/Audio_Example.lua"/>
</folder>
<folder name="Bird_Animation">
<file source="examples/Bird_Animation/gfx/bird_black_01.png"/>
<file source="examples/Bird_Animation/gfx/bird_black_02.png"/>
<file source="examples/Bird_Animation/gfx/bird_black_03.png"/>
<file source="examples/Bird_Animation/gfx/bird_white_01.png"/>
<file source="examples/Bird_Animation/gfx/bird_white_02.png"/>
<file source="examples/Bird_Animation/gfx/bird_white_03.png"/>
<file source="examples/Bird_Animation/gfx/sky_world.png"/>
<file source="examples/Bird_Animation/bird.lua"/>
<file source="examples/Bird_Animation/Bird_Animation.lua"/>
</folder>
<folder name="Button_Example">
<file source="examples/Button_Example/button_down.png"/>
<file source="examples/Button_Example/button_up.png"/>
<file source="examples/Button_Example/Button_Example.lua"/>
</folder>
</folder>
<folder name="libs">
<file source="libs/button.lua"/>
<file source="libs/scenemanager.lua"/>
</folder>
<file source="main.lua"/>
<file source="start.lua"/>
</project>
Binary file added examples/Audio_Example/1-down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Audio_Example/1-up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Audio_Example/1.wav
Binary file not shown.
Binary file added examples/Audio_Example/2-down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Audio_Example/2-up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Audio_Example/2.wav
Binary file not shown.
Binary file added examples/Audio_Example/3-down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Audio_Example/3-up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Audio_Example/3.wav
Binary file not shown.
31 changes: 31 additions & 0 deletions examples/Audio_Example/Audio_Example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--[[
This is an example of sound capabilities of Gideros Studio
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
]]

Audio_Example = Core.class(Sprite)

function Audio_Example:init()

local s1 = SoundButton.new("examples/Audio_Example/1-up.png", "examples/Audio_Example/1-down.png", "examples/Audio_Example/1.wav")
local s2 = SoundButton.new("examples/Audio_Example/2-up.png", "examples/Audio_Example/2-down.png", "examples/Audio_Example/2.wav")
local s3 = SoundButton.new("examples/Audio_Example/3-up.png", "examples/Audio_Example/3-down.png", "examples/Audio_Example/3.wav")

s1:setPosition(10, 50)
s2:setPosition(110, 50)
s3:setPosition(210, 50)

self:addChild(s1)
self:addChild(s2)
self:addChild(s3)


local label = TextField.new(nil, "click buttons to play sounds") -- we pass first parameter as nil to use the system font
label:setPosition(86, 170)
self:addChild(label)

end
19 changes: 19 additions & 0 deletions examples/Audio_Example/soundbutton.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--[[
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
]]

SoundButton = Core.class(Sprite)

function SoundButton:init(upimage, downimage, soundfile)
local up = Bitmap.new(Texture.new(upimage))
local down = Bitmap.new(Texture.new(downimage))
local button = Button.new(up, down)

self:addChild(button)

local sound = Sound.new(soundfile)

button:addEventListener("click", function() sound:play() end)
end

42 changes: 42 additions & 0 deletions examples/Bird_Animation/Bird_Animation.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--[[
A frame by frame bird animation example
The old frame is removed by Sprite:removeChild and the new frame is added by Sprite:addChild
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
]]

Bird_Animation = Core.class(Sprite)

function Bird_Animation:init()

-- load texture, create bitmap from it and set as background
local background = Bitmap.new(Texture.new("examples/Bird_Animation/sky_world.png"))
self:addChild(background)

-- these arrays contain the image file names of each frame
local frames1 = {
"examples/Bird_Animation/bird_black_01.png",
"examples/Bird_Animation/bird_black_02.png",
"examples/Bird_Animation/bird_black_03.png"}

local frames2 = {
"examples/Bird_Animation/bird_white_01.png",
"examples/Bird_Animation/bird_white_02.png",
"examples/Bird_Animation/bird_white_03.png"}

-- create 2 white and 2 black birds
local bird1 = Bird.new(frames1)
local bird2 = Bird.new(frames1)
local bird3 = Bird.new(frames2)
local bird4 = Bird.new(frames2)

-- add birds to the stage
self:addChild(bird1)
self:addChild(bird2)
self:addChild(bird3)
self:addChild(bird4)

end
68 changes: 68 additions & 0 deletions examples/Bird_Animation/bird.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
--[[
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
--]]

Bird = Core.class(Sprite)

function Bird:init(frameList)
-- create a Bitmap for each frame
self.frames = {}
for i = 1, #frameList do
self.frames[i] = Bitmap.new(Texture.new(frameList[i]))
end

self.nframes = #frameList

-- add first Bitmap as a child
self.frame = 1
self:addChild(self.frames[1])

-- subframe is used to adjust the speed of animation
self.subframe = 0

-- set initial position
self:setPosition(540, math.random(160) + 40)

-- set the speed of the bird
self.speedy = math.random(-500, 500) / 1000
self.speedx = math.random(2000, 4000) / 1000

self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end

function Bird:onEnterFrame()
self.subframe = self.subframe + 1

-- for every 10 frames, we remove the old frame and add the new frame as a child
if self.subframe > 10 then
self:removeChild(self.frames[self.frame])

self.frame = self.frame + 1
if self.frame > self.nframes then
self.frame = 1
end

self:addChild(self.frames[self.frame])

self.subframe = 0
end

-- get the current position
local x,y = self:getPosition()

-- change the position according to the speed
x = x - self.speedx
y = y + self.speedy

-- if the bird is out of the screen, set new speed and position
if x < -100 then
self.speedy = math.random(-500, 500) / 1000
self.speedx = math.random(2000, 4000) / 1000
x = 540
y = math.random(160) + 40
end

-- set the new position
self:setPosition(x, y)
end
Binary file added examples/Bird_Animation/gfx/bird_black_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Bird_Animation/gfx/bird_black_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Bird_Animation/gfx/bird_black_03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Bird_Animation/gfx/bird_white_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Bird_Animation/gfx/bird_white_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Bird_Animation/gfx/bird_white_03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Bird_Animation/gfx/sky_world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions examples/Button_Example/Button_Example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--[[
This example demonstrates a generic Button class
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
]]

Button_Example = Core.class(Sprite)

function Button_Example:init()

-- create a label to show number of clicks
local label = TextField.new(nil, "Clicked 0 time(s)")
label:setPosition(120, 240)
self:addChild(label)

-- create the up and down sprites for the button
local up = Bitmap.new(Texture.new("examples/Button_Example/button_up.png"))
local down = Bitmap.new(Texture.new("examples/Button_Example/button_down.png"))

-- create the button
local button = Button.new(up, down)

-- register to "click" event
local click = 0
button:addEventListener("click",
function()
click = click + 1
label:setText("Clicked " .. click .. " time(s)")
end)

button:setPosition(40, 150)
self:addChild(button)

end
Binary file added examples/Button_Example/button_down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/Button_Example/button_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions examples/Button_Example/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--[[
This example demonstrates a generic Button class
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
]]

-- create a label to show number of clicks
local label = TextField.new(nil, "Clicked 0 time(s)")
label:setPosition(120, 240)
stage:addChild(label)

-- create the up and down sprites for the button
local up = Bitmap.new(Texture.new("button_up.png"))
local down = Bitmap.new(Texture.new("button_down.png"))

-- create the button
local button = Button.new(up, down)

-- register to "click" event
local click = 0
button:addEventListener("click",
function()
click = click + 1
label:setText("Clicked " .. click .. " time(s)")
end)

button:setPosition(40, 150)
stage:addChild(button)
104 changes: 104 additions & 0 deletions libs/button.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
--[[
A generic button class
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile
]]

Button = Core.class(Sprite)

function Button:init(upState, downState)
self.upState = upState
self.downState = downState

self.focus = false

self:updateVisualState(false)

self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)

self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)
self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)
self:addEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self)
end

function Button:onMouseDown(event)
if self:hitTestPoint(event.x, event.y) then
self.focus = true
self:updateVisualState(true)
event:stopPropagation()
end
end

function Button:onMouseMove(event)
if self.focus then
if not self:hitTestPoint(event.x, event.y) then
self.focus = false;
self:updateVisualState(false)
end
event:stopPropagation()
end
end

function Button:onMouseUp(event)
if self.focus then
self.focus = false;
self:updateVisualState(false)
self:dispatchEvent(Event.new("click"))
event:stopPropagation()
end
end

-- if button is on focus, stop propagation of touch events
function Button:onTouchesBegin(event)
if self.focus then
event:stopPropagation()
end
end

-- if button is on focus, stop propagation of touch events
function Button:onTouchesMove(event)
if self.focus then
event:stopPropagation()
end
end

-- if button is on focus, stop propagation of touch events
function Button:onTouchesEnd(event)
if self.focus then
event:stopPropagation()
end
end

-- if touches are cancelled, reset the state of the button
function Button:onTouchesCancel(event)
if self.focus then
self.focus = false;
self:updateVisualState(false)
event:stopPropagation()
end
end

-- if state is true show downState else show upState
function Button:updateVisualState(state)
if state then
if self:contains(self.upState) then
self:removeChild(self.upState)
end

if not self:contains(self.downState) then
self:addChild(self.downState)
end
else
if self:contains(self.downState) then
self:removeChild(self.downState)
end

if not self:contains(self.upState) then
self:addChild(self.upState)
end
end
end
Loading

0 comments on commit f089d9c

Please sign in to comment.