Allright, I tried both suggestions:
> Using the _G.score and _G.scoreText unfortunately didn´t change the error.
> I implented both print statements, both fired perfectly on both collissions.
The problem still is that the changing scoreText overlaps with the 0 score, which doesn´t dissapear ;- (.
I have other global variables to fire the bullet, and they work fine.
I´ll paste the entire code below, if you would come up with something be sure to let me know !
– Main.lua
[lua]_W = display.contentWidth
_H = display.contentHeight
local director = require (“director”)
local mainGroup = display.newGroup()
local function main ()
mainGroup:insert(director.directorView)
director:changeScene(“menu”)
return true
end
main ()[/lua]
– Menu.lua
[lua]module(…,package.seeall)
function new()
local localGroup = display.newGroup()
local bg = display.newImageRect(“images/bg.png”,_W,_H)
bg:setReferencePoint(display.CenterReferencePoint)
bg.x = _W/2
bg.y =_H/2
local play_btn = display.newImageRect(“images/play_btn.png”, _W/10,75)
play_btn:setReferencePoint(display.CenterReferencePoint)
play_btn.x = _W/2
play_btn.y = _H/2
play_btn.scene = “game”
function changeScene(e)
if (e.phase == “ended”) then
director:changeScene(e.target.scene)
end
end
localGroup:insert(bg)
localGroup:insert(play_btn)
play_btn:addEventListener(“touch”, changeScene)
return localGroup
end[/lua]
– game.lua
[lua]module(…,package.seeall)
function new ()
local localGroup = display.newGroup()
_W = display.contentWidth
_H = display.contentHeight
velocity = 70
halfpoint = 0
local physics = require(“physics”)
physics.start( true )
local bullets = {}
local function spawnBullets(total)
for i = 1, total do
bullets[i] = bullet.newBullet()
– flag bullets for removal later
bullets[i].remove = true
end
end
local bullet = require(“bullet”)
local bar = require (“bar”)
local basket = require (“basket”)
spawnBullets(5)
return localGroup
end[/lua]
– Bullet.lua
[lua]module(…, package.seeall)
function newBullet()
local bullet = display.newImage(“images/bullet.png”)
bullet.x = 100; bullet.y = _H - 100
force_multiplier = 4
score = 0
scoreText = display.newText( " " … score,0, 15, Helvetica, 50)
– set up some properties
bullet.myName = “bullet”
physics.addBody(bullet, “kinematic”, {density = 0.2, friction = 0.2, bounce = 0.5, radius = 20})
bullet.linearDamping = 0.3
bullet.angularDamping = 0.8
bullet.isBullet = true – force continuoius collision detection, to stop really fast shots from moving through
–bullet.isSensor = true
bullet.isLoaded = false
function bullet:touch(e)
local t = e.target
if (e.phase == “began”) then
display.getCurrentStage():setFocus(t)
self.isFocus = true
self.bodyType = “kinematic”
– Stop current motion if any
self:setLinearVelocity(0,0)
self.angularVelocitiy = 0
myLine = nil
elseif(self.isFocus) then
if(e.phase == “moved”)then
if(myLine)then
myLine.parent:remove(myLine)
end
myLine = display.newLine(self.x,self.y,e.x,e.y)
myLine:setColor(255255,255,50)
myLine.width =8
elseif(e.phase == “ended” or e.phase == “cancelled”)then
display.getCurrentStage():setFocus(nil)
self.isFocus=false
–audio.play(shot)
if(myLine)then
myLine.parent:remove(myLine)
end
– Launch the bullet
self.bodyType=“dynamic”
self:applyForce((self.x - e.x)*force_multiplier, (self.y - e.y)*force_multiplier, self.x,self.y)
function bullet:collision (event)
if event.other.myName == “bar” then
bullet.isLoaded = true
print("bullet.isLoaded is: ", bullet.isLoaded, “colliding with”, event.other.myName)
end
if bullet.isLoaded == true and event.other.myName == “basket” then
score = score + 10
scoreText.text = " " …score
print(“Hey, I just updated the score. It’s now:”, score)
end
end
end
end
end
bullet:addEventListener(“touch”, bullet)
bullet:addEventListener(“collision”, bullet)
Runtime:addEventListener(“enterFrame”,bullet)
return bullet
end[/lua]
[import]uid: 126207 topic_id: 29363 reply_id: 118034[/import]