Thanks for all the responses.
I am used to programming a game loop in which things happen even when there is no user input.
I wanted to use 4 movement sequences of a mouse running North, South, East, West and move the sprite around the screen by setting its x y and switching the sequence associated with the current direction and randomly switching for North/South to East/West.
I used a game loop in a repeat until in which this action takes place but when run the screen is blank and nothing is seen.
I programmed a counter so I could stop the loop after a number of iterations and when stopped the screen shows what I expected.
I should be able to stop the run by pressing esc key as I have a listener checking for that key and it sets the value of the until clause.
sample uses coroutine but same problem.
I wanted a delay so I could see the screen action but am unable to do that.
Having read the response about Corona not working that way (no wait or sleep or even release to the system) so it looks like I might need to look for another development tool.
copy of code here:
–
– main.lua
– run a mouse round the screen until user presses esc
myaudio = audio.loadSound(“mousesqueak.mp3”)
SCREEN_WIDTH = display.contentWidth
SCREEN_HEIGHT = display.contentHeight
direction = “S”
xstep=40
ystep=20
xplot=0
yplot=0
xpos=0
ypos=0
index=0
numFrames=3
frameSize=128
msg=""
options = {width=128,height=128,numFrames=12,sheetContentWidth=385,sheetContentHeight=512}
image = graphics.newImageSheet(“mouse.png”,system.ResourceDirectory,options)
myRect = display.newRect(SCREEN_WIDTH/2,SCREEN_HEIGHT/2,SCREEN_WIDTH,SCREEN_HEIGHT)
paint = {0}
– sequences tables
Move = {
– consecutive frames sequence
{
name = “Move”,
start = 6,
count = 2,
time = 1000,
loopCount = 0,
loopDirection = “bounce”
}
}
South = {
– consecutive frames sequence
{
name = “South”,
start = 1,
count = 3,
time = 1000,
loopCount = 0,
loopDirection = “bounce”
}
}
West = {
– consecutive frames sequence
{
name = “West”,
start = 4,
count = 3,
time = 1000,
loopCount = 0,
loopDirection = “bounce”
}
}
East = {
– consecutive frames sequence
{
name = “East”,
start = 7,
count = 3,
time = 1000,
loopCount = 0,
loopDirection = “bounce”
}
}
North = {
– consecutive frames sequence
{
name = “North”,
start = 4,
count = 3,
time = 1000,
loopCount = 0,
loopDirection = “bounce”
}
}
mainGroup = display.newGroup()
mainGroup.x = 0
mainGroup.y = 0
msgGroup = display.newGroup()
mouseMove = display.newSprite(mainGroup, image, Move )
mouseSouth = display.newSprite(mainGroup, image, South )
mouseWest = display.newSprite(mainGroup, image,West )
mouseEast = display.newSprite(mainGroup, image,East )
mouseNorth = display.newSprite(mainGroup, image,North )
loopit = true
mouse = display.newSprite(mainGroup,image,South)
mouse.x = display.contentCenterX
mouse.y = display.contentHeight - 100
mouse:setSequence(mouseMove)
xpos = mouse.x
ypos = mouse.y-ystep
mouse.y = ypos
mouse:play()
xplot = mouse.x
xpos = xplot
yplot = mouse.y
ypos = yplot
msg = “Start: xplot=” … tostring(xplot) … " yplot=" … tostring(yplot)
showmsg = display.newText(msgGroup, msg, 200, 80, native.systemFont, 36 )
–usleep(10000)
display.remove(msgGroup)
msgGroup = display.newGroup()
myRect:setFillColor(0.5)
myRect:setStrokeColor(1,0,0)
local function onKeyEvent(event)
if (event.keyName == “esc”) then
loopit=false
return true
end
return false
end
– Add the key event listener
Runtime:addEventListener( “key”, onKeyEvent )
gameLoop = coroutine.create(function()
repeat
msg = “Loop: " … tostring(loopCount) … " xplot=” … tostring(xplot) … " yplot=" … tostring(yplot)
showmsg = display.newText( msgGroup, msg, 220, 80, native.systemFont, 36 )
msg = “Screen: width=” … tostring(SCREEN_WIDTH) … " height=" … tostring(SCREEN_HEIGHT)
showmsg = display.newText(msgGroup, msg, 220, 140, native.systemFont, 36 )
msg = “Mouse: x=” … tostring(mouse.x) … " y=" … tostring(mouse.y)
showmsg = display.newText(msgGroup, msg, 220, 200, native.systemFont, 36 )
if (direction==“S”) then
mouse.x = xpos
mouse.y = ypos
yplot=yplot+ystep
if (yplot>SCREEN_HEIGHT) then
yplot=yplot-ystep
direction=“N”
ypos=yplot
mouse.x = xpos
mouse.y = ypos
mouse:setSequence(“North”)
end
if (yplot<(SCREEN_HEIGHT/2) and (math.random() >0.95)) then
direction=“E”
audio.play(myaudio)
mouse:setSequence(“East”)
end
ypos = yplot
mouse.y = ypos
mouse:play()
elseif (direction == “N”) then
mouse.x = xpos
mouse.y = ypos
yplot=yplot-ystep
if (yplot<0) then
yplot=yplot+ystep
direction=“S”
mouse:setSequence(“South”)
Ypos=Yplot
mouse.Y = Ypos
end
ypos=yplot
mouse.y = ypos
mouse:play()
elseif (direction == “E”) then
mouse.x = xpos
mouse.y = ypos
xplot=xplot+xstep;
if (xplot>SCREEN_WIDTH) then
xplot=xplot-xstep
direction=“W”
mouse.x = xplot
mouse.y = ypos
mouse:setSequence(“West”)
xpos=xplot
end
if ((xplot<SCREEN_WIDTH/2) and (math.random() >0.9)) then
direction=“N”
audio.play(myaudio)
mouse:setSequence(“North”)
xpos=xplot
mouse.x = xpos
end
xpos = xplot
mouse.x = xpos
mouse:play()
elseif (direction == “W”) then
mouse.x = xpos
mouse.y = ypos
xplot=xplot-xstep;
if (xplot<0) then
xplot=xplot+xstep
direction=“E”
mouse:setSequence(“East”)
end
xpos=xplot
mouse.x = xpos
mouse:play()
end
display.remove(msgGroup)
msgGroup = display.newGroup()
until loopit==false
mouse:pause()
end)
print(gameLoop)