Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Levels. — Gideros Forum

Levels.

i_Wattsi_Watts Member
edited January 2015 in General questions
Starting scene lua.
StartScene = Core.class(Sprite)
 
self.onLevel = 1 
 
function StartScene:init()
 
	sceneButton = {}
	for i = 1, 9 do 
	sceneButton[i] = Bitmap.new(Texture.new("images/button.png"))
	sceneButton[i]:setScale(0.2, 0.2)
	self:addChild(sceneButton[i])
	end 
 
	sceneButton[1]:setPosition( (conf.maxx/2-sceneButton[1]:getWidth()/2) /4 + 20 , 20)
	sceneButton[4]:setPosition((conf.maxx/2-sceneButton[2]:getWidth()/2) / 4 + 20, conf.maxy/2-sceneButton[2]:getHeight()/2)
	sceneButton[7]:setPosition((conf.maxx/2-sceneButton[3]:getWidth()/2) / 4 + 20, conf.maxy-sceneButton[3]:getHeight()-20)
 
	------
 
	sceneButton[2]:setPosition(conf.maxx/2-sceneButton[1]:getWidth()/2, 20)
	sceneButton[5]:setPosition(conf.maxx/2-sceneButton[2]:getWidth()/2, conf.maxy/2-sceneButton[2]:getHeight()/2)
	sceneButton[8]:setPosition(conf.maxx/2-sceneButton[3]:getWidth()/2, conf.maxy-sceneButton[3]:getHeight()-20)
 
	------
 
	sceneButton[3]:setPosition((conf.maxx/2-sceneButton[1]:getWidth()/2) * 1.65, 20)
	sceneButton[6]:setPosition((conf.maxx/2-sceneButton[2]:getWidth()/2) * 1.65, conf.maxy/2-sceneButton[2]:getHeight()/2)
	sceneButton[9]:setPosition((conf.maxx/2-sceneButton[3]:getWidth()/2) * 1.65, conf.maxy-sceneButton[3]:getHeight()-20)
 
	numberText = {}
for i = 1, 9 do 
 
	numberText[i] = TextField.new(nil, i) 
	numberText[i]:setScale(3)
	self:addChild(numberText[i])
	numberText[i]:setVisible(false)
end 
 
numberText[1]:setPosition( (conf.maxx/2-numberText[1]:getWidth()/2) /4 + 43,70 )
numberText[2]:setPosition( (conf.maxx/2-numberText[1]:getWidth()/2) /4 + 173,70 )
numberText[3]:setPosition( (conf.maxx/2-numberText[1]:getWidth()/2) /4 + 303,70 )
-- 
numberText[4]:setPosition( (conf.maxx/2-numberText[1]:getWidth()/2) /4 + 43,172 )
numberText[5]:setPosition( (conf.maxx/2-numberText[1]:getWidth()/2) /4 + 173,172 )
numberText[6]:setPosition( (conf.maxx/2-numberText[1]:getWidth()/2) /4 + 303,172 )
--
numberText[7]:setPosition( (conf.maxx/2-numberText[1]:getWidth()/2) /4 + 43,270 )
numberText[8]:setPosition( (conf.maxx/2-numberText[1]:getWidth()/2) /4 + 173,270 )
numberText[9]:setPosition( (conf.maxx/2-numberText[1]:getWidth()/2) /4 + 303,270 )
 
self:addEventListener(Event.MOUSE_DOWN, self.btnClick, self)
 
end
 
for i = 1, 9 do 
if self.onLevel >= i then 
numberText[i]:setVisible(true)
end
end 
 
function StartScene:btnClick(event)
	local x = event.x
	local y = event.y
 
	for i = 1, 9 do 
	if sceneButton[i]:hitTestPoint(x, y) and numberText[i]:isVisible() then	
	event:stopPropagation()
	sceneManager:changeScene("level1", conf.transitionTime, conf.transition, conf.easing) -- "level[i]" ? 
	end 
	end 
end
level 1 lua
	if self.points == 60 then 	
	--self.onLevel = self.onLevel + 1
	sceneManager:changeScene("start", conf.transitionTime, conf.transition, conf.easing) -- "level[i]" ? 
 
	end
I have a few questions
------------------
1. when I run the program, it crashed, because of the self.onLevel in the start lua. I am trying to set it up so that after 60 points is reached, is switches to the second scene.
2.sceneManager:changeScene("level1", conf.transitionTime, conf.transition, conf.easing) how would you say level[i], instead of level 1? Thanks for the help

Comments

  • piepie Member
    use < pre lang= "lua" > code < /pre > without spaces to format code. ;)

    1 - you could use Timer(), os.timer() or Event.ENTERFRAME to add 1 on each call.
    I would go for timer class so that you can choose the speed of your counter.
    --create timer: 1000 milliseconds, 5 times = 5 seconds
    local onLevelTimer = Timer.new(1000, 5)
    --set initial counter value
    local counter = 0
     
    local function endTimer() --this is called on TIMER COMPLETE
    	print("Timer End", counter)
    end
     
    local function onTime() --this is called at the end of every repeat
    	counter = counter+1
    	print("ON TIME", counter)
    end
     
    --event listeners
    onLevelTimer:addEventListener(Event.TIMER, onTime)
    onLevelTimer:addEventListener(Event.TIMER_COMPLETE, endTimer)
     
    --start the timer
    onLevelTimer:start()
    2 - I am not sure I understood the question: I think you should be able to use level[i] if you have a "level" table with a valid index value.
  • Will this change the level after a certain amount of time though? I want it to switch levels after a certain amount of points is reached, so a timer wouldn't work.
  • piepie Member
    edited January 2015
    I'm sorry I misunderstood your first question either.. :)
    my example would change level on timer events, given the appropriate scenemanager call.
    Actually only once if it is placed inside a scene -because changing scene should destroy the first one, that contains the timer ;) .

    Coming to the answer of your question, (I hope :) ) you need something to check if and when 60 points are reached: you can use an enterframe event, and your guess in the first post seems fine to me.
     
    function level1:onEnterFrame()
    	if self.points == 60 then 	
     	--If self.onLevel keeps track of "current level number", it should be increased by 1
     	--to go to the next one
     	self.onLevel = self.onLevel + 1 
    	sceneManager:changeScene(level[self.onLevel], conf.transitionTime, conf.transition, conf.easing)  
    	end
    end
     
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    If my assumption on self.onLevel is right, and this scene ( level[number]) is available to scenemanager, I suppose it should work.
  • everything works accept the start function. it doesnt like the line self.onLevel
  • piepie Member
    edited January 2015
    Ah, because it is outside of the init function.
    self doesn't exist here.
    Try this:
    function StartScene:init() 
            self.onLevel = 1 
    	sceneButton = {}
    	for i = 1, 9 do 
    ....
  • It didn't like that either
  • piepie Member
    I'd say it's the same thing as before: the for statement (for i = 1, 9 do ) is outside of any scope of StartScene, thus there is no self to point to, nor self.onLevel.

    keeping my previous suggestion about self.onLevel, try also including the for statement inside init()
    self:addEventListener(Event.MOUSE_DOWN, self.btnClick, self)
     
    --end  -- move this (line 53)
     
    for i = 1, 9 do 
    if self.onLevel >= i then 
    numberText[i]:setVisible(true)
    end
    end 
     
    end --here (line 61)
    and please be a little more specific about the errors you get :)
Sign In or Register to comment.