Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Layer or z-index; programming practice — Gideros Forum

Layer or z-index; programming practice

mefistomefisto Member
edited December 2012 in Code snippets
Yesterday i was hardly working on our first game, i made a very simple mistake when i was playing with layer class, it was a sleepless night :) . I think there are few sample code to show how can we achieve layer or z-index approach. I don't know, may be gideros tutorials have some!
So , i decided to share a simple code snippet to show a sample. I hope the new comers may benefits from it .
require "box2d"
 
game = gideros.class(Sprite)
 
function game:init()
	self.world= b2.World.new(0,0.1,true)
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
	self:addEventListener("exitBegin", self.onExitBegin, self)
	self.layers={}
	self.layers[1]=Sprite.new()
	self.layers[2]=Sprite.new()
	self.layers[3]=Sprite.new()
	self.layers[4]=Sprite.new()
 
	self:addChild(self.layers[1])
	self:addChild(self.layers[2])
	self:addChild(self.layers[3])
	self:addChild(self.layers[4])
 
 
	self:createBall(1,100,50)
	self:createBall(2,150,50)
	self:createBall(3,200,50)
	self:createBall(4,250,50)	
 
	self:createBall(3,100,10)
	self:createBall(4,150,10)
	self:createBall(1,200,10)
	self:createBall(2,250,10)		
 
	self:createBodilessBall(4,100,80)
	self:createBodilessBall(3,150,80)
	self:createBodilessBall(2,200,80)
	self:createBodilessBall(1,250,80)	
 
	self:createBodilessBall(1,115,90)
	self:createBodilessBall(2,165,90)
	self:createBodilessBall(3,215,90)
	self:createBodilessBall(4,265,90)		
 
end
 
 
function game:createBall(layerNo,px,py)
	local bbody = self.world:createBody{type = b2.DYNAMIC_BODY,position ={x=px, y=py}}
	local shotball = Bitmap.new(Texture.new("images/ball"..layerNo..".png"))
	local bradius = shotball:getWidth()/2
	local bcircle = b2.CircleShape.new(0, 0, bradius)
	local bfixture = bbody:createFixture{shape = bcircle,density = 0.7, restitution = 0.6, friction = 0.2, filter = {categoryBits = 2, maskBits = 1}}
	shotball:setAnchorPoint(0.5,0.5)
	bbody:setAngle(shotball:getRotation() * math.pi/180)
	shotball.body = bbody
	shotball.body.type = "ball"
	shotball:setPosition(px,py)
	self.layers[layerNo]:addChild(shotball)	
end
 
function game:createBodilessBall(layerNo,px,py)
	local shotball = Bitmap.new(Texture.new("images/ball"..layerNo..".png"))
	shotball:setPosition(px,py)
	self.layers[layerNo]:addChild(shotball)	
end
 
function game:onEnterFrame(event) 
	self.world:step(1/60, 10,8 )
	for j = 1, #self.layers do
		for i = 1, self.layers[j]:getNumChildren() do
			local sprite = self.layers[j]:getChildAt(i)
			if sprite.body then
				local body = sprite.body
				local bodyX, bodyY = body:getPosition()
				sprite:setPosition(bodyX, bodyY)
				sprite:setRotation(body:getAngle() * 180 / math.pi)
			end
		end
	end
end
 
 
function game:onExitBegin()
  self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end
rar
rar
Denver2.rar
20K

Likes: ar2rsawseen

Tagged:
+1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.