Вопрос

I am newbie to programming and especially to Corona SDK (Lua). I need a help! The problem is: I have 10 images in the array and a button while tap the button i need to remove the previous image and show the next image stored in array. I did all ,but while tap for next image the next image is comes nicely, but the previous image is not removed from screen, I want to remove it, and one more thing is after completing 10th image , I like to start from image 1, like a loop.

local Next = function()

    for j = 1, 10 do

        j=j+1

    end

    return true

end

local dotted =  {"images/1.png", "images/2.png","images/3.png","images/4.png","images/5.png",
                "images/6.png","images/7.png","images/8.png","images/9.png","images/10.png"}


local nextButton = widget.newButton{
    left = display.contentWidth/1.25,
    top = display.contentHeight - 55,
    defaultFile="images/next.png",
    width = 50, height = 50,
    onRelease = Next}


j = 1 
function loadingImages1()       
    di = display.newImageRect(dotted[j],150,300);
    di.x = calcx(40,"PER")  
    di.y = calcx(30,"PER")
    di.height = calch(60,"PER")
    di.width = calcw(20,"PER")
    j = j + 1
end

local function onObjectTap( self,event )
    --di1.removeSelf();
    di1:removeSelf();
    loadingImages1()
    return true
end
nextButton:addEventListener( "tap", onObjectTap )
Это было полезно?

Решение

I don't think you need the Next function. You should by di1 you mean di then removeSelf() should be sufficient for it to disappear from view. Also I'm not seeing any code to initialize the di before first tap. You should have something like

local nextIndex = 1

local dotted = {....}

local di -- avoid globals

local function loadingImages1()       
    di = display.newImageRect(dotted[nextIndex],150,300);
    ... set x, y, height, width; then: 
    -- update next index, cycle back to 1 if necessary
    nextIndex = nextIndex + 1
    if nextIndex > #dotted then 
        nextIndex = 1 
    end
end

loadingImages1() -- run once to initialize

local function onObjectTap( self,event )
    di:removeSelf() -- remove di
    loadingImages1()
    return true
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top