Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
My pong game is laggy ): — Gideros Forum

My pong game is laggy ):

HarrisonHarrison Member
edited February 2014 in General questions
I made a pong game as my 2nd decent game (my 1st was a reaction time game) but it is very laggy. And when i look at the more complex games, i think: How could my pong game be laggier than their >500 lines of code games?!
something is wrong... i am trying to fix the lag
anyone know what is causing this?
if you dont want to wade through my code i totally understand
i would post a .gproj if a knew how to.
set fps to unlimited on player.


main.lua
 
--all X and Y variables will be CAPS
--landscape left
--THE "VARIABLE BANK"
 
--ball
ballX=160
ballY=240
Xspeed=0.2
Yspeed=0.2
paddlebX=160
paddlebY=25
paddlebspeed=0.3
win=false
 
--
 
--IMAGES
 
--background:
local  backgroundtexture = Texture.new("images/background.png")
local background = Bitmap.new(backgroundtexture)
background:setPosition(0,0)
stage:addChild(background)
--end of background
 
--ball
local balltexture = Texture.new("images/ball.png")
local ball = Bitmap.new(balltexture)
ball:setPosition(ballX,ballY)
stage:addChild(ball)
--end of ball
 
local paddletexture = Texture.new("images/paddle.png")
 paddlea = Bitmap.new(paddletexture)
 paddlea:setAnchorPoint(0.5,0.5)
paddlea:setPosition(160,455)
local paddle = paddle.new(paddlea)
stage:addChild(paddle)
 
--make the AI's paddle
paddleb = Bitmap.new(paddletexture)
paddleb:setAnchorPoint(0.5,0.5)
paddleb:setPosition(160,25)
stage:addChild(paddleb)
 
 
 
 
 
 
function onEnterFrame(event)
 
--put the ball into motion with "speed"
--all of the increasing speed math is turned off for now, because the ball goes too fast
if ballX > 290 then
Xspeed = Xspeed * -1
--Xspeed=Xspeed-0.01
--Yspeed=Yspeed+0.01
elseif ballX < 12 then 
Xspeed = Xspeed * -1
--Yspeed=Yspeed+0.01
elseif ballY > 465 then 
Yspeed = Yspeed * -1 
--Xspeed=Xspeed+0.01
--Yspeed=Yspeed+0.01
elseif ballY < 0 then --used to be 25 
Yspeed = Yspeed * -1
--Xspeed=Xspeed+0.01
--Yspeed=Yspeed+0.01
end
 
ballX=ballX+Xspeed
ballY=ballY+Yspeed
ball:setX(ballX)
ball:setY(ballY)
 
paddlehittop = paddlea:hitTestPoint(ballX, ballY+7)
 paddlehitbottom = paddlea:hitTestPoint(ballX, ballY-7)
 paddlehitleft=paddlea:hitTestPoint(ballX-7, ballY)
 paddlehitright=paddlea:hitTestPoint(ballX+7, ballY)
 paddlehittoprightcorner=paddlea:hitTestPoint(ballX+7, ballY+7)
 paddlehittopleftcorner=paddlea:hitTestPoint(ballX-7, ballY+7)
 paddlehitbottomleftcorner=paddlea:hitTestPoint(ballX+7, ballY-7)
 paddlehitbottomrightcorner=paddlea:hitTestPoint(ballX-7, ballY-7)
 if paddlehitbottom==true or paddlehittop==true or paddlehitbottomrightcorner==true or paddlehitbottomleftcorner==true or  paddlehittopleftcorner==true or paddlehittoprightcorner==true then 
 Yspeed = Yspeed * -1 
 print("the ball has been hit!!")
 elseif paddlehitleft==true or paddlehitright==true then
 Xspeed = Xspeed * -1
 end
  --paddleb:
paddlebhittop = paddleb:hitTestPoint(ballX, ballY+7)
 paddlebhitbottom = paddleb:hitTestPoint(ballX, ballY+3)
 paddlebhitleft=paddleb:hitTestPoint(ballX-7, ballY)
 paddlebhitright=paddleb:hitTestPoint(ballX+7, ballY)
 paddlebhittoprightcorner=paddleb:hitTestPoint(ballX+7, ballY+7)
 paddlebhittopleftcorner=paddleb:hitTestPoint(ballX-7, ballY+7)
 paddlebhitbottomleftcorner=paddleb:hitTestPoint(ballX+7, ballY+3)
 paddlebhitbottomrightcorner=paddleb:hitTestPoint(ballX-7, ballY+3)
 if paddlebhitbottom==true then print("bottom!!") end
 if paddlebhitbottom==true or paddlebhittop==true then--or paddlebhitbottomrightcorner==true or paddlebhitbottomleftcorner==true or  paddlebhittopleftcorner==true or paddlebhittoprightcorner==true then 
 Yspeed = Yspeed * -1 
 print("the ball has been hit!!")
 elseif paddlebhitleft==true or paddlebhitright==true then
 Xspeed = Xspeed * -1
 end
 
 
 
if ballY < 150 then
--paddleX=paddleb:getX()
 
print(paddleX)
--paddleX=ballX--------
if paddlebX > ballX then 
print("less than!!")
paddlebX=paddlebX-paddlebspeed
elseif paddlebX < ballX then
print("more than!!")
paddlebX=paddlebX+paddlebspeed
end
 
if paddlebX> 200 then --275
paddlebX=200
elseif paddlebX < 175 then --45
paddleX=175
end
print("2--"..paddlebX)
paddleb:setX(paddlebX)
--
end
end
 
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
-----------------------------------------------------paddle.lua
paddle = Core.class(Sprite)
function paddle:init(sprite)
print("initializing paddle")
self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
self:addEventListener(Event.MOUSE_MOVE, onMouseMove, self)
self:addEventListener(Event.MOUSE_UP, onMouseUp, self)
self:addChild(sprite) 
 
end
function paddle:onMouseDown(event)
if self:hitTestPoint(event.x,event.y) then
 
print("you clicked me!!")
print(event.x)
paddlepos=event.x
self.paddlefocus=true
end
end
 function onMouseMove(self, event)
	if self.paddlefocus==true then
 
		dx =  event.x 
		if dx < 45 then 
dx = 45
elseif dx > 275 then
dx = 275
end
		-- - self.x0
 
 
		--[[self:setX(self:getX() + dx)
		self:setY(self:getY() + dy)
 
		self.x0 = event.x
		self.y0 = event.y 
 
 
 
		paddle:setX(self:getX() + dx)
		]]--
		--print(dx)
 
		paddlea:setX(dx)
	end
end
 function onMouseUp(self, event)
	if self.paddlefocus==true then
		self.paddlefocus = false
	end
end
--------------------------------------
--[[function paddlehitcheck(paddlechoice)
self.paddlehittop = paddlechoice:hitTestPoint(ballX, ballY+7)
 self.paddlehitbottom = paddlechoice:hitTestPoint(ballX, ballY-7)
 self.paddlehitleft=paddlechoice:hitTestPoint(ballX-7, ballY)
 self.paddlehitright=paddlechoice:hitTestPoint(ballX+7, ballY)
 self.paddlehittoprightcorner=paddlechoice:hitTestPoint(ballX+7, ballY+7)
 self.paddlehittopleftcorner=paddlechoice:hitTestPoint(ballX-7, ballY+7)
 self.paddlehitbottomleftcorner=paddlechoice:hitTestPoint(ballX+7, ballY-7)
 self.paddlehitbottomrightcorner=paddlechoice:hitTestPoint(ballX-7, ballY-7)
 if self.paddlehitbottom==true or self.paddlehittop==true or self.paddlehitbottomrightcorner==true or self.paddlehitbottomleftcorner==true or  self.paddlehittopleftcorner==true or self.paddlehittoprightcorner==true then 
 Yspeed = Yspeed * -1 
 print("the ball has been hit!!")
 elseif self.paddlehitleft==true or self.paddlehitright==true then
 Xspeed = Xspeed * -1
 end
 end
 ]]--
“ The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ” - Tom Cargill

Comments

  • your code looks good enough to run at 60 fps and it is not possible in android or ios device to run at more than 60 fps. can you tell which device you are using

    :)
  • HarrisonHarrison Member
    edited February 2014
    PC, i dont think this will ever reach the app store. Just a practice project. Could it be that i am running
    paddlebhittop = paddleb:hitTestPoint(ballX, ballY+7)
    paddlebhitbottom = paddleb:hitTestPoint(ballX, ballY+3)
    paddlebhitleft=paddleb:hitTestPoint(ballX-7, ballY)
    paddlebhitright=paddleb:hitTestPoint(ballX+7, ballY)
    paddlebhittoprightcorner=paddleb:hitTestPoint(ballX+7, ballY+7)
    paddlebhittopleftcorner=paddleb:hitTestPoint(ballX-7, ballY+7)
    paddlebhitbottomleftcorner=paddleb:hitTestPoint(ballX+7, ballY+3)
    paddlebhitbottomrightcorner=paddleb:hitTestPoint(ballX-7, ballY+3)
    at more than 60 times per second? perhaps i will adjust the math to run at 60 fps cap...
    “ The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ” - Tom Cargill
  • or 30. I am glad you think it would run at 60 fps. I thought my project was dead and useless because i was doing something wrong.
    “ The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ” - Tom Cargill
  • hgvyas123hgvyas123 Guru
    Accepted Answer
    this code should not have problem on pc but lets try to optimize this how about to chk collision with below function
    function Sprite:collidesWith(sprite2)
    	local x,y,w,h = self:getBounds(stage)
    	local x2,y2,w2,h2 = sprite2:getBounds(stage)
     
    	return not ((y+h < y2) or (y > y2+h2) or (x > x2+w2) or (x+w < x2))
    end
    and then if they had collided chk x and y position of both objects to see form which side collision occurs. it will be little bit faster than current

    :)

    Likes: Harrison

    +1 -1 (+1 / -0 )Share on Facebook
  • hgvyas123hgvyas123 Guru
    edited February 2014
    also can you run some example projects shipped with gideros if they are also not running at good fps then you may need to install some graphics driver else it is a problem with code and need to optimize that
  • john26john26 Maintainer
    Did you try it on an actual device? I've noticed real devices run a lot faster than the Windows simulator.

    Likes: Harrison

    +1 -1 (+1 / -0 )Share on Facebook
  • eezingeezing Member
    edited February 2014 Accepted Answer
    I see a couple of things that could hinder performance a bit.

    First, many of your variables and all of your functions are global. Lua reference manual states globals should only be used when absolutely necessary. The reason is because locals are not only safer, but considerably faster. Locals are stored in registers on the stack for quick access; globals are not. Use locals for all of your variables, functions and tables if possible.

    Second, seems like you have a lot of code (arguments more specifically) inside the onEnterFrame listener. Basically, if it doesn't need to be calculated or checked on every frame then probably a good idea to take it out. But, considering the style of game much of this stuff has to be calculated on every frame, which leads to the next...

    Box2d would be a great option for a game like pong. I admit that Box2d is a bit foreign at first, but implementing it into your game could be a huge benefit. Box2d contains very efficient algorithms required for stuff like movement and collisions, and more importantly, those algorithms are in binary form... binary is king when performance is the priority.

    So, the quick thing to try is change everything to local if possible. Then remove anything you can out of your enter frame listener. Good luck!
Sign In or Register to comment.