I have encountered a problem.
I've been searching for a few hours on how to move around an image on screen when someone touches a button.
local ui = require("ui")
local background = display.newImage("ramitsplash.png")
local play = display.newImage("ramitplaybutton.png", 440, 580)
-- Define the touch listener
local function touchListener( event )
local bkgnd = display.newImage("ramitlevel1bkgnd.png")
local shooter = display.newImage("ramitshooter.png")
shooter.x = 501
shooter.y = 300
local moveup = display.newImage("ramitmoveup.png", 900, 575)
end
local function touchListener1( event )
shooter.x = 501 + 10
end
play:addEventListener( "touch", touchListener )
moveup:addEventListener( "touch", touchListener1 )
shooter.x = 501 + 10 is the line I'm having trouble with. It says something about 'Attempt to index global value ( a nil value)'.
Anyone know how to move the image at the touch of a button?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| local ui = require("ui")
local background = display.newImage("ramitsplash.png")
-- create images and hide them before you need them for better performance
local bkgnd = display.newImage("ramitlevel1bkgnd.png")
bkgnd.isVisible = false
local shooter = display.newImage("ramitshooter.png")
shooter.isVisible = false
-- ui module's button listener
local function moveupBtnListener(event)
if event.phase == "release" then
shooter.x = 511
end
end
-- this is how you create a button with ui module
local moveupBtn = ui.newButton{
defaultSrc = "ramitmoveup.png",
defaultX = 60, -- width of ramitmoveup
defaultY = 60, -- height of ramitmoveup
overSrc = "ramitmoveup-over.png", -- create an clicked ramitmoveupimage, you can just use ramitmoveup.png
overX = 60, -- width of ramitmoveup
overY = 60, -- height of ramitmoveup
onEvent = moveupBtnListener,
id = "moveupButton",
emboss = false
}
moveupBtn.x = 900
moveupBtn.y = 575
moveupBtn.isVisible = false
local function playBtnListener(event)
if event.phase == "release" then
bkgnd.isVisible = true
shooter.isVisible = true
shooter.x = 501
shooter.y = 300
moveupBtn.isVisible = true
end
end
local playBtn = ui.newButton{
defaultSrc = "ramitplaybutton.png",
defaultX = 60, -- width of ramitplaybutton
defaultY = 60, -- height of ramitplaybutton
overSrc = "ramitplaybutton-over.png", -- create an clicked ramitplaybutton image, you can just use ramitplaybutton.png
overX = 60, -- width of ramitplaybutton
overY = 60, -- height of ramitplaybutton
onEvent = playBtnListener,
id = "playButton",
emboss = false
}
playBtn.x = 440
playBtn.y = 580 |
I tried the code, but it comes up with another 'attempt to index global 'ui' (a nil value)' error in line 16. What does that mean and how do I fix it?
isn't there a sample code called drag me that you can look at as well?
c.
with regards to the ui error -- you need to have ui.lua in your project folder, try getting it from ghosts vs monsters
I have the ui.lua file in the folder with all the other stuff. What is that global indexing error?
do you have
1
| local ui = require("ui") |
I have that line.
I'm fed up with this. This is not easy for newcomers like everyone said it was.
actually, on the contrary, corona is remarkably simple to pick up for beginners...especially compared with trying to learn obj-c -- it just takes some time to get your head around
try removing
id = "moveupButton",
and
id = "playButton",
make sure you don't have any typo or I didn't make any mistakes in the code
views:1506 update:2011/10/10 9:00:20