Hi all,
just had some spare time and have been playing with Corona, am trying to follow the very excellent tutorials from techority.com, what I’m trying to do is create an microswitch arcade style joystick with the ui class from one of Peaches tute’s . . . I figured out how to get buttons to have roll over states and play audio - e.g. a click sound - now I would like to do the same for the joystick . . . basically an eight way joystick with the ball and stem that " clicks " at each increment and goes back to center when the user is not touching it - similar to the analogue examples throughout the site - this is driving me nuts !
If anyone can have a looksee at the code and give us a few pointers that would be great . . . I downloaded this tutee file from techority.com and doctored it up a bit . . . I wanna make a shmup for the first game but I need to understand code and how it works . . . trying to think logically with very very minimum coding background . . . will pick it up eventually.
I was thinking of using an invisible button that triggers a roll over state which would position the joystick ( yep I was trying to cheat - old school Macromedia Director user here ) - but it ain’t working how I thought it was going to work - so by touch the joystick follows the left thumb and clicks at each of the 8 increments or microswitches when he user is not touching the joystick bit, the ball clicks back to the center - man I miss " tell target " syntax you could use it for almost anything to do with ui and graphics switching in Director . . .
Thanks in advance peeps !
[code]
– SCENE SETUP –
display.setStatusBar (display.HiddenStatusBar)
– Hides the status bar
– LOAD CLASSES –
require “ui”
–[[
Here we require the ui.lua, which we need to do a “mouse over” type button.
You can copy ui.lua directly from this project folder into your own, or find it in some of the
sample projects available in Applications/CoronaSDK/SampleCode.
–]]
– LOAD GRAPHICS –
local background = display.newImage (“assets/images/background.png”)
background.x = 380
background.y = 500
– Sets the background
local viper = display.newImage (“assets/images/viper.png”)
viper.x = 380
viper.y = 500
– Puts the usual picture into the app and positions it
– JOYSTICK –
local joystickplate = display.newImage (“assets/images/joystick_base.png”)
joystickplate.x = 104
joystickplate.y = 922
local joystickdef = display.newImage (“assets/images/joystick_ball.png”)
joystickdef.x = 104
joystickdef.y = 922
– ARROWS –
local up = display.newImage (“assets/images/upx.png”)
–up.alpha = 0.5
up.x = 104
up.y = 873
local down = display.newImage (“assets/images/downx.png”)
down.x = 104
down.y = 975
local left = display.newImage (“assets/images/leftx.png”)
left.x = 50
left.y = 920
local right = display.newImage (“assets/images/rightx.png”)
right.x = 155
right.y = 920
– Puts in all four movement arrow images and positions them
– BUTTONS –
– Simple button with rollover & audio –
–local upbut = ui.newButton{
upbut = ui.newButton{
default = “assets/images/invisble.png”,
over = “assets/images/ball_up.png”,
–onEvent = buttonHandler,
–onPress = buttonHandler,
–alpha = 0.5
–id = “up”,
x = 104,
y = 873,
}
redbutton = ui.newButton{
default = “assets/images/but_red_up.png”,
over = “assets/images/but_red_down.png”,
x = 484,
y = 965,
}
yellowbutton = ui.newButton{
default = “assets/images/but_yellow_up.png”,
over = “assets/images/but_yellow_down.png”,
x = 538,
y = 874,
}
greenbutton = ui.newButton{
default = “assets/images/but_green_up.png”,
over = “assets/images/but_green_down.png”,
x = 614,
y = 802,
}
bluebutton = ui.newButton{
default = “assets/images/but_blue_up.png”,
over = “assets/images/but_blue_down.png”,
x = 707,
y = 756,
}
– Playing sound on button touchevent –
– BUTTON CLICK AUDIO –
local click = media.newEventSound (“assets/audio/click.wav”)
– 1 Calling in audio
local function playclick (event)
media.playEventSound (click)
end
– 2 Function to play audio
upbut:addEventListener (“touch”, playclick)
redbutton:addEventListener (“touch”, playclick)
yellowbutton:addEventListener (“touch”, playclick)
greenbutton:addEventListener (“touch”, playclick)
bluebutton:addEventListener (“touch”, playclick)
– 3 Listener to trigger sound - can happen on any event - collison / position etc.
– MOVE VIPER –
local motionx = 0
local motiony = 0
local speed = 10
– Speed can be adjusted here to easily change how fast viper moves. Very convenient!
local function stop (event)
if event.phase ==“ended” then
motionx = 0
motiony = 0
end
end
Runtime:addEventListener(“touch”, stop )
– When no arrow is pushed, this will stop viper from moving.
local function moveviper (event)
viper.x = viper.x + motionx
viper.y = viper.y + motiony
end
Runtime:addEventListener(“enterFrame”, moveviper)
– When an arrow is pushed, this will make me move.
–function up:touch()
function up:touch()
motionx = 0
motiony = -speed
end
up:addEventListener(“touch”, up)
function down:touch()
motionx = 0
motiony = speed
end
down:addEventListener(“touch”, down)
function left:touch()
motionx = -speed
motiony = 0
end
left:addEventListener(“touch”,left)
function right:touch()
motionx = speed
motiony = 0
end
right:addEventListener(“touch”,right)
– The above four functions are stating the arrows should all listen for touches and defining
– the way I should move based on each touch.
local function stage (event)
if viper.x < 50 then
viper.x = 50
end
if viper.x > 718 then
viper.x = 718
end
if viper.y > 800 then
viper.y = 800
end
if viper.y < 50 then
viper.y = 50
end
if speed < 0 then
speed = 0
end
end
Runtime:addEventListener(“enterFrame”, stage)
– This will keep me from going off the sides of the screen. It’s not actually a wrap, that would
– involve me disappearing and popping back on the opposite side, but I’m in the habbit of calling
– whatever function I use to keep the main “hero” on screen “stage”, and I doubt that will change any
– time soon.[/code]
[import]uid: 12724 topic_id: 17140 reply_id: 317140[/import]