Created
June 3, 2013 17:35
-
-
Save simsaens/5699814 to your computer and use it in GitHub Desktop.
Simple Scene Switching - Put the tabs in the following order: Main, Scene, BoxScene, CircleScene, Button
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BoxScene = class(Scene) | |
function BoxScene:init() | |
-- We have to initialize our | |
-- super class | |
Scene.init(self) | |
end | |
function BoxScene:draw() | |
background(78, 99, 51, 255) | |
fill(195, 154, 154, 255) | |
rectMode(CENTER) | |
rect(WIDTH/2,HEIGHT/2,300,300) | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CircleScene = class(Scene) | |
function CircleScene:init() | |
-- We have to initialize our | |
-- super class | |
Scene.init(self) | |
end | |
function CircleScene:draw() | |
background(51, 56, 99, 255) | |
fill(154, 181, 195, 255) | |
ellipse(WIDTH/2,HEIGHT/2,300,300) | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- SimpleScenes | |
-- Use this function to perform your initial setup | |
function setup() | |
-- Create two different scenes, these are subclasses | |
-- of the Scene class | |
boxScene = BoxScene() | |
circleScene = CircleScene() | |
-- Add a button to box scene to change to circle scene | |
circleButton = Button("Circle Scene", | |
vec2(WIDTH/2, 50), | |
function() currentScene = circleScene end) | |
boxScene:addButton(circleButton) | |
-- Add a button to circle scene to change to box scene | |
boxButton = Button("Box Scene", | |
vec2(WIDTH/2, 50), | |
function() currentScene = boxScene end) | |
circleScene:addButton(boxButton) | |
-- currentScene represents the scene we are currently | |
-- drawing. We can switch scenes by assigning a new value | |
-- to this variable | |
currentScene = boxScene | |
end | |
-- This function gets called once every frame | |
function draw() | |
currentScene:drawScene() | |
end | |
function touched(touch) | |
currentScene:touched(touch) | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Scene = class() | |
function Scene:init() | |
self.buttons = {} | |
end | |
function Scene:addButton(button) | |
self.buttons[button] = button | |
end | |
function Scene:removeButton(button) | |
self.buttons[button] = nil | |
end | |
function Scene:drawScene() | |
pushStyle() | |
-- Draw our scene content | |
self:draw() | |
-- Now draw all our buttons | |
for k,btn in pairs(self.buttons) do | |
btn:draw() | |
end | |
popStyle() | |
end | |
function Scene:draw() | |
end | |
function Scene:touched(touch) | |
-- Check if any buttons are touched | |
for k,btn in pairs(self.buttons) do | |
if btn:containsPoint(touch.x, touch.y) then | |
btn:touched(touch) | |
return | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment