Ask attempt index global "kupu" <a nil value>

--  
-- Project: button.png  
-- Description:   
--  
-- Version: 1.0  
-- Managed with http://CoronaProjectManager.com  
--  
-- Copyright 2013 . All Rights Reserved.  
--   
  
--  
-- Project: TA  
-- Description:   
--  
-- Version: 1.0  
-- Managed with http://CoronaProjectManager.com  
--  
-- Copyright 2013 . All Rights Reserved.  
--   
local storyboard = require ( "storyboard" )  
local sprite = require ("sprite")  
--local movieclip =require("movieclip")  
  
local widget = require ( "widget" )  
local scene = storyboard.newScene()  
  
local function listener(event)  
 if event.target.name == "kupu" and event.phase =="began" then  
 kupu:prepare("kupuGerak")  
  
 kupu:play()  
 end  
end  
local function Tombolprev(event)  
 if event.phase == "release" then  
  
 storyboard.gotoScene( "menu", "fade", 800 )  
 return true  
 end  
end  
  
local function tombolnext(event)  
 if event.phase == "release" then  
 storyboard.gotoScene("halaman1-2","fade",800)  
 return true  
 end  
end  
function scene:createScene( event )  
 local halaman1Scene = self.view  
 local latar = display.newImage ("latarHal1.png");  
 halaman1Scene:insert(latar);  
 ----------------------------------------  
 local tabelCerita = display.newImage( "table.png" );  
 tabelCerita.x = 512;  
 halaman1Scene:insert(tabelCerita);  
 ----------------------------------------  
 --local rusa = display.newImage ("rusa.png");  
 local rusaSheet = sprite.newSpriteSheet ( "sprite-rusa.png", 395, 377 )  
 local rusaSet = sprite.newSpriteSet ( rusaSheet, 1, 4 )  
 sprite.add ( rusaSet, "rusaGerak", 1, 4, 1000, 0 )  
 local rusa = sprite.newSprite(rusaSet)  
 rusa.x = 750;  
 rusa.y = 400;  
 rusa.name = "rusa"  
 --rusa:prepare("rusaGerak")  
 --rusa:play()  
 halaman1Scene:insert(rusa);  
 ----------------------------  
 local kayu = display.newImage ("kayu.png");  
 kayu.x = 690;  
 kayu.y = 550;  
 halaman1Scene:insert(kayu);  
 local rumput = display.newImage ("rumput.png")  
 rumput.x = 950;  
 rumput.y = 540;  
 halaman1Scene:insert(rumput);  
 --local kelinci = display.newImage ("kelinci.png");  
 ---------------------------------  
 local kelinciSheet = sprite.newSpriteSheet ( "spritekelinci.png", 159, 239 )  
 local kelinciSet = sprite.newSpriteSet ( kelinciSheet, 1, 6 )  
 sprite.add(kelinciSet,"Kjalan",1,6,1000,0)  
 local kelinci = sprite.newSprite(kelinciSet)  
 kelinci.x = 150;  
 kelinci.y = 430;  
 kelinci.name = "kelinci"  
 --kelinci:prepare("KelinciJalan")  
 --kelinci:play()  
 halaman1Scene:insert(kelinci);  
 -----------------------------------  
  
 local kuraSheet = sprite.newSpriteSheet ("sprite-kura.png",165,222)  
 local kuraSet = sprite.newSpriteSet ( kuraSheet, 1, 8 )  
 sprite.add(kuraSet,"kuraGerak",1,8,1000,0)  
 local kura = sprite.newSprite(kuraSet)  
 kura.x = 350;  
 kura.y = 460;  
 kura.name = "kura"  
 --kura:prepare("kuraGerak")  
 --kura:play()  
 halaman1Scene:insert(kura);  
 -------------------------------------  
 --local kupu = display.newImage ("kupu.png");  
 local kupuSheet = sprite.newSpriteSheet ( "sprite-kupu.png", 110, 104 )  
 local kupuSet = sprite.newSpriteSet ( kupuSheet, 1, 4 )  
 sprite.add ( kupuSet, "kupuGerak", 1, 4, 1000, 0 )  
 local kupu = sprite.newSprite(kupuSet)  
 kupu.x = 860;  
 kupu.y = 230;  
 kupu.name = "kupu"  
 --kupu:prepare("kupuGerak")  
 --kupu:play()  
  
 halaman1Scene:insert(kupu);  
 -------------------------------------------------  
 local tabBar = display.newImage ("tabBar.png");  
 tabBar.y = 550;  
 halaman1Scene:insert(tabBar);  
  
end  
  
function scene:enterScene( event )  
local Halaman1EnterScene = self.view  
 storyboard.purgeScene( "menu" )  
kupu:addEventListener ( "touch", listener )  
 Halaman1EnterScene:insert(kupu);  
  
 -------------------------------  
 local tomPrev = widget.newButton{  
 default = "prevBtn.png",  
 over = "prevBtn-over.png",  
 onRelease = Tombolprev;  
 }  
 tomPrev.y = 550;  
 Halaman1EnterScene:insert(tomPrev);  
 local tomNext = widget.newButton {  
 default = "nextBtn.png",  
 over = "nextBtn-over.png",  
 onRelease = tombolnext;  
 }   
 tomNext.x = 977;  
 tomNext.y = 550;  
 Halaman1EnterScene:insert(tomNext);  
end  
function scene:exitScene( event )  
local group = self.view  
 display.remove(widget);  
 -- kupu:removeEventListener ( "touch", kupu )  
 --kupu:prepare()  
 -- kelinci:removeEventListener( "touch", kelinci )  
 -- kelinci:prepare("Kjalan")  
end  
  
function scene:destroyScene( event )  
 local group = self.view  
 -- kupuSheet:dispose()  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
return scene  
  

halo my name adi from indonesia,i am having problem Error code in lua :127 kupu:addEventListener ( "touch", listener ) , how do i fix the problem?,problem is attemp index global “kupu”

*sorry if my english languange is bad :frowning: [import]uid: 150663 topic_id: 36280 reply_id: 336280[/import]

line 110 in the code above local kupu = sprite.newSprite(kupuSet) you are setting it as a local variable so it’s only available in that function (scene:createScene). You need to define it as a global variable so you can access it outside the scene:createScene function, replace line 110 with this:

kupu = sprite.newSprite(kupuSet) [import]uid: 58885 topic_id: 36280 reply_id: 144143[/import]

line 110 in the code above local kupu = sprite.newSprite(kupuSet) you are setting it as a local variable so it’s only available in that function (scene:createScene). You need to define it as a global variable so you can access it outside the scene:createScene function, replace line 110 with this:

kupu = sprite.newSprite(kupuSet) [import]uid: 58885 topic_id: 36280 reply_id: 144143[/import]

line 110 in the code above local kupu = sprite.newSprite(kupuSet) you are setting it as a local variable so it’s only available in that function (scene:createScene). You need to define it as a global variable so you can access it outside the scene:createScene function, replace line 110 with this:

kupu = sprite.newSprite(kupuSet) [import]uid: 58885 topic_id: 36280 reply_id: 144143[/import]

line 110 in the code above local kupu = sprite.newSprite(kupuSet) you are setting it as a local variable so it’s only available in that function (scene:createScene). You need to define it as a global variable so you can access it outside the scene:createScene function, replace line 110 with this:

kupu = sprite.newSprite(kupuSet) [import]uid: 58885 topic_id: 36280 reply_id: 144143[/import]