Why doesn't my setFocus work?

Hey guys,

I can’t work this one out and man have I tried. If the argument is true then I want the attackButton to be the only thing the user can select. They have to hold it down otherwise the attack finishes. It semi-works but only if the user keeps their finger directly over the button when they release or presses the button then lets their finger roam bringing it back to rest directly over attack button then release. Also my joystick is still selectable the whole time. Not good, I want the joystick disabled.

Any help would be greatly appreciated

Here I create the attackButton

[lua]—Create Attack Button

attackButton = ui.newButton{
defaultSrc = “AttackButton.png”,
defaultX = 51,
defaultY = 51,
overSrc = “Attack-Button-Pressed.png”,
overX = 51,
overY = 51,
onEvent = attackButtonHandler,
id = “attackButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = “”,
emboss = false
}

attackButton.x = 420; attackButton.y = 250;

hudGroup:insert(attackButton)[/lua]
Here is it onEvent Function

[lua]function attackButtonHandler(event)

if event.phase == “press” and smallShip1HitPossible == true and attackPossible == true then

local attackButton = event.target

player.isVisible = false
player.isActive = false
smallShip1.isVisible = false
display.getCurrentStage():setFocus( attackButton, event.id )

local reducesmallShip1Health = function()

if smallShip1HitPossible == true and smallShip1Health >= 1 then
smallShip1Health = smallShip1Health -10

elseif smallShip1HitPossible == true and smallShip1Health <= 1 then

timer.cancel(smallShip1CollisionTimer)
player.isVisible = true
attacksheet:dispose()
smallShip1:removeSelf()
timer.cancel(playerCollisionTimer)
attackPossible = false
smallShip1HitPossible = false

end

end

smallShip1CollisionTimer = timer.performWithDelay(500, reducesmallShip1Health,0)

attacksheet = sprite.newSpriteSheet(“attack.png”, 55, 169)

attackset = sprite.newSpriteSet (attacksheet, 1, 2)
sprite.add (attackset, “grab”, 1, 2, 300, 0)
sprite.add (attackset, “attack”, 2, 2, 300, 0)

newAttack = sprite.newSprite (attackset)
newAttack.x = smallShip1.x
newAttack.y = smallShip1.y
newAttack:prepare(“grab”)
newAttack:play(“attack”)

camera:insert(newAttack)

elseif event.phase == “release” and attackPossible == true then

display.getCurrentStage():setFocus(attackButton, nil)

smallShip1HitPossible=false
if smallShip1HitPossible==false then
if(smallShip1CollisionTimer~=nil) then – just to be safe
timer.cancel(smallShip1CollisionTimer)
end
end

player.isVisible = true
smallShip1.isVisible = true

attacksheet:dispose()

end [/lua] [import]uid: 26289 topic_id: 13631 reply_id: 313631[/import]

setFocus() requires just one parameter, why do you pass it two?

the way to setFocus is setFocus(control)
and to remove the focus is setFocus(nil)

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13631 reply_id: 50056[/import]

Hey jayantv. I’ve tried multiple ways. Do you mean like this? Because I’ve tried lots of different ways.

Cheers for the response

[lua]elseif event.phase == “press” and smallShip2HitPossible == true and attackPossible == true then

event.target = attackButton
display.getCurrentStage():setFocus(event.target)
player.isVisible = false
player.isActive = false
smallShip2.isVisible = false
smallShip2IsHit = true

local reducesmallShip2Health = function()

if smallShip2HitPossible == true and smallShip2Health >= 1 then
smallShip2Health = smallShip2Health -10

elseif smallShip2HitPossible == true and smallShip2Health <= 1 then

timer.cancel(smallShip2CollisionTimer)
player.isVisible = true
attacksheet2:dispose()
smallShip2:removeSelf()
timer.cancel(playerCollisionTimer)
attackPossible = false
smallShip2HitPossible = false

end

end

local diffSpace = 60

smallShip2CollisionTimer = timer.performWithDelay(500, reducesmallShip2Health,0)

attacksheet2 = sprite.newSpriteSheet(“attack.png”, 55, 169)

attackset2 = sprite.newSpriteSet (attacksheet2, 1, 2)
sprite.add (attackset2, “grab”, 1, 2, 300, 0)
sprite.add (attackset2, “attack”, 2, 2, 300, 0)

newAttack2 = sprite.newSprite (attackset2)
newAttack2.x = smallShip2.x
newAttack2.y = smallShip2.y + diffSpace
newAttack2:prepare(“grab”)
newAttack2:play(“attack”)

camera:insert(newAttack2)

end

if event.phase == “release” and smallShip2IsHit == true and attackPossible == true then

display.getCurrentStage():setFocus(nil)
smallShip2IsHit = false
smallShip2HitPossible=false
if smallShip2HitPossible==false then
if(smallShip2CollisionTimer~=nil) then
timer.cancel(smallShip2CollisionTimer)
end
end

player.isVisible = true
smallShip2.isVisible = true

attacksheet2:dispose()
[/lua] [import]uid: 26289 topic_id: 13631 reply_id: 50064[/import]