Okay so I've been helped overcome a TextureBase problem and one other problem. Now We have a third. I decided I'd make a new post for this question as my question on my previous post had been answered.
So here's the error:
main.lua:65: attempt to index global 'Pool' (a nil value)
stack traceback:
main.lua:65: in main chunk
Again here is my full code:
local sound = Sound.new("MakoBeamNCSGiderosCompRemix(-32Db).wav")
local channel = sound:play()
application:setBackgroundColor(0x0)
width = application:getContentWidth()
local username = ""
local textInputDialog = TextInputDialog.new("Player1",
"Enter Name Player1", username, "Cancel", "Save")
backgroundtexture = Texture.new("BGComp.png")
backgroundtexture = Bitmap.new(backgroundtexture)
stage:addChild(backgroundtexture)
backgroundtexture:setAnchorPoint(0.5,0.5)
backgroundtexture:setRotation(90)
backgroundtexture:setPosition(239,160)
backgroundtexture:setScaleX(1.75)
backgroundtexture:setScaleY(1.75)
local retrofont = TTFont.new("8-BIT WONDER.TTF", 20, true)
local text = TextField.new(retrofont,username)
text:setPosition(0, 20)
stage:addChild(text)
text:setTextColor(0xfffffff)
text:setScale(1.0)
text:setPosition(10,20)
bullet = Texture.new("bullet.png")
bullet = Bitmap.new(bullet)
stage:addChild(bullet)
bullet:setScale(0.3)
bullet:setPosition(0,0)
bullet:setRotation(-90)
playership1 = Bitmap.new(Texture.new("ship.png"))
stage:addChild(playership1)
playership1:setAnchorPoint(0.5,0.5)
playership1:setPosition(245,300)
playership1:setScale(1.0)
--playership1:setColorTransform(0.1,0.9,0.3)
local function onComplete(e)
--if e.buttonIndex then
username = e.text
text:setText(username)
--end
end
textInputDialog:addEventListener(Event.COMPLETE, onComplete)
textInputDialog:show()
left = false
right = false
shipX=width/2
playerDir=0
bDir=0
by= 1
bullets = {}
counter = 1
bpool = Pool.new()
function Pool:init()
self.pool = {}
end
function Pool:createObject()
local b
if #self.pool > 0 then
b = table.remove(self.pool)
else
b = Bitmap.new(Texture.new("bullet.png", true))
b:setScale(0.3)
b:setPosition(0,0)
b:setRotation(-90)
end
return b
end
function Pool:destroyObject(b)
b:removeFromParent()
table.insert(#self.pool, b)
end
function gameloop(e)
shipX=shipX + (playerDir* 5)
if shipX<20 then
shipX = 20
elseif
shipX>(width-20) then
shipX = width - 20
end
playership1:setX(shipX)
by=by+(bDir*5)
bullet:setY(by)
end
stage:addEventListener(Event.ENTER_FRAME,gameloop)
stage:addEventListener(Event.KEY_DOWN,function(e)
if e.keyCode==KeyCode.LEFT then
left=true
playerDir=-1
elseif e.keyCode==KeyCode.RIGHT then
right=true
playerDir=1
elseif e.keyCode==KeyCode.Z then
print("fire")
bx,by=playership1:getPosition()
bullets[counter] = bpool:createobject()
stage:addChild(bullets[counter])
bullets[counter]:setPosition(bx,by)
bDir=-1
counter = counter+1
end
end)
stage:addEventListener(Event.KEY_UP,function(e)
if e.keyCode==KeyCode.LEFT then
left=false
if right then
playerDir=1
else
playerDir=0
end
elseif e.keyCode==KeyCode.RIGHT then
right=false
if left then
playerDir=-1
else
playerDir=0
end
end
end)
Once again I hope we can overcome this issue and I hope it will be the last.
Thank you for your time.
(BTW I was told how to highlight the code but I'm doing something wrong because nothings happening)
Comments
Sand image ).you missed somewhere the Pool class definition
how does it know what Pool should be?
So now were back to my first problem... It's giving me an error when I press the fire key Z.
But do not fear it's a different one so we're definitely getting there.
The Error is:
there is no "createobject" method in Pool definition ( no Pool:createobject(), but Pool:createObject() ).
it should be:
the most important thing though is that you understand "why"
with : sign you call a method from a class:
bpool is an instance of Pool class, which has the method createObject(), which returns the bullet (creating a new one, or drawing it out from self.pool if it's already there ) .
bullets[counter] = bpool:createObject()
assign to bullets[counter] what is returned from Pool:createObject() method.
It works and I can fire but... well you need to see for your self (fire button is Z press it and you'll know what I mean). This really is bizarre...
Copy this code and add these files into a Gideros File:
Huge thanks to everyone who has helped so far.
I have run over the firing code
and is moving the bullet in bDir=-1). I would assume that it has something to do with the pool we created earlier but other than that I am clueless.
there you go, I wrote comments here and there, feel free to ask what you don't get
As a best practice, I found that learning to close a function or a statement before writing inside it reduces % to have this error. And using "a lot" of TAB key if you're writing nested things, helps dramatically to spot this kind of mistakes.
But honestly I can't thank you enough for all the help you've given me. You have taken time out of your life to help me.
I am extremely grateful.
I expect you'll be seeing me on the forums again at some point, but until then, THANK YOU! :D:D:D:D:D:D:D:D:D:D
the first is the end to if..else
the second is the end to for..do
the third is the end to gameloop.
I'm glad you managed to sort it out (with a lot of help from @pie ), I think you need to look at what the code does and try figure out why it does what it does. Save a good copy somewhere (and print it out?) then if you make a mistake you can compare it to what you have and see where you went wrong.
EOF mean end of file btw (basically you hit then end when you shouldn't have done).
(re the spam... I deleted your other thread you started btw so you didn't get multiple answers)
https://deluxepixel.com
BTW thanks for removing my posts as I was going to, but had no clue how. also thanks for the EOF info
Likes: SinisterSoft