Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Trouble with Cameraposition and Controls

edited September 2017 in Questions Posts: 9

Hi,
I am beginner and I have modified some code from this forum to create my own project.
But I have some trouble with a button that doesn't work right when the navigation class is active.
I come from Germany and it is difficult for me to discribe the problem more detailed.
I would be very happy if someone could help me with my problem and make my code more effektive.
The project code was to long for the forum. I posted it on Github.
Sorry for the language mistakes that I may have done.
Thank you, Gewis

Link to Github project:
https://gist.github.com/Gfox21/02122908f384a4ef2b1768e33f0ef2a1

Comments

  • dave1707dave1707 Mod
    edited September 2017 Posts: 7,468

    @Gewis I don't have time right now to look thru your code, but here's an example of code I have that shows a menu with buttons that take you to different websites. When you're in a website and want to get back to the program, press done in the upper left of the screen. When I run your code, all I get is a blue screen with three words on it, Start, Settings, and Website. Tapping them does nothing. I'll look thru your code later to see what needs to be done.

    EDIT: Sorry, I was in such a hurry I forgot to post the example. Here it is.

    displayMode(FULLSCREEN)
    
    function setup() 
        func=menu    -- start screen
    
        buttonTab={}    -- buttons table
    
        -- button(x,y,width,height,button name,button screen,button function)
    
        -- buttons that show on the menu screen
        table.insert(buttonTab,button(350,600,100,50,"Screen 1",menu,screen1))
        table.insert(buttonTab,button(350,500,100,50,"Screen 2",menu,screen2))
        table.insert(buttonTab,button(350,400,100,50,"Screen 3",menu,screen3))
        table.insert(buttonTab,button(700,50,100,50,"EXIT",menu,close))
    
        -- buttons that show on screen1
        table.insert(buttonTab,button(350,600,150,50,"Google",screen1,url1))
        table.insert(buttonTab,button(350,500,150,50,"twolivesleft",screen1,url2))
        table.insert(buttonTab,button(350,100,150,50,"Menu",screen1,menu))
    
        -- buttons that show on screen2
        table.insert(buttonTab,button(350,600,150,50,"map quest",screen2,url3))
        table.insert(buttonTab,button(350,100,150,50,"Menu",screen2,menu))
    
        -- buttons that show on screen3
        table.insert(buttonTab,button(350,600,150,50,"amazon",screen3,url4))    
        table.insert(buttonTab,button(350,100,150,50,"Menu",screen3,menu))
    end
    
    function draw()
        background(40, 40, 50)
        fill(255)
        func()    -- call the function set by the button
    end
    
    function touched(t)    -- check if a button was pressed
        if t.state==BEGAN then
            for a,b in ipairs(buttonTab) do
                if b:touched(t) then
                    break
                end                
            end
        end
    end
    
    function menu()
        background(58, 97, 136, 255)
        fontSize(30)
        text("Menu Screen",350,700)
        drawButtonTab()
    end
    
    function screen1()
        background(101, 43, 43, 255)
        fontSize(30)
        text("Screen 1",350,700)
        drawButtonTab()
    end
    
    function screen2()
        background(110, 165, 74, 255)
        fontSize(30)
        text("Screen 2",350,700)
        drawButtonTab()
    end
    
    function screen3()
        background(150, 144, 44, 255)
        fontSize(30)
        text("Screen 3",350,700)
        drawButtonTab()
    end
    
    function url1()
        openURL('http://www.google.com',true)
        func=screen1    -- return to screen1
    end
    
    function url2()
        openURL('http://twolivesleft.com',true)
        func=screen1    -- return to screen2
    end
    
    function url3()
        openURL('http://www.mapquest.com',true)
        func=screen2    -- return to screen3
    end
    
    function url4()
        openURL('http://www.amazon.com',true)
        func=screen3    -- return to screen4
    end
    
    function drawButtonTab()    -- draw selected buttons from button table
        fontSize(20)
        for a,b in ipairs(buttonTab) do
            b:draw()
        end
    end    
    
    button=class()
    
    function button:init(x,y,w,h,name,screen,func)
        self.x=x    -- x position
        self.y=y    -- y position
        self.w=w    -- width
        self.h=h    -- height
        self.name=name    -- name to put on the button 
        self.screen=screen -- screen to draw the button on
        self.func=func    -- function to call when the button is pressed
    end
    
    function button:draw()
        if func==self.screen then    -- draw the button
            sprite("Cargo Bot:Dialogue Button",self.x,self.y,120)
            fill(255, 0, 0, 255)
            text(self.name,self.x,self.y)
        end
    end
    
    function button:touched(t)
        if func==self.screen then    -- only check current screen button 
            if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
                t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
                    func=self.func
                    return true
            end
            return false
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 7,468

    @Gewis Just something you should know for future code that you post. Anytime you use something in your Documents or Dropbox folder, only you can use them. Anyone who tries to run your code doesn't have access to them, so any Sprites or whatever you use will not show up when your code runs. When I run your code, the only thing I see is a blue background screen with three words on it, Start, Settings, and Website. They don't respond to touch because whatever is supposed to be in Documents or Dropbox isn't there, so the width and height of them are 0 so I can't touch within a button with 0 width and height. Don't worry about any language mistakes because if I had to respond in German, I wouldn't be able to.

  • dave1707dave1707 Mod
    Posts: 7,468

    @Gewis I changed any reference to Documents buttons to something I have. I can run your code and it responds to the touches. The program cancels when I select Website because Browser:setup isn't defined anywhere. I'm not sure what you're trying to do with it.

  • dave1707dave1707 Mod
    Posts: 7,468

    @Gewis You're not using classes correctly. You create a Navigation class but instead of creating an instance to it, you're calling the Navigation functions directly.

  • dave1707dave1707 Mod
    Posts: 7,468

    @Gewis I think I got your program working far enough to get to a 3000x3000 grid with a Back button on it that moves with the grid. Pressing the back button doesn't work, but pressing an area to the lower left of the screen center does the Back response. I'm assuming that's the problem you're referring to.

  • dave1707dave1707 Mod
    Posts: 7,468

    @Gewis If what I mentioned above is the problem you're referring to, here's how to fix it. You're moving the whole screen by using translate, which is also causing the button to move. You need to add a push and pop matrix to seperate the Back button from the moving screen. Here's the changes.

         elseif screen == 8 then
            pushMatrix()
            Navigation:draw()
            popMatrix()
            --prov1:draw()
            button14:draw()
            text(back_button,button14.pos.x,button14.pos.y)        
         end
    
  • edited September 2017 Posts: 9

    @dave1707 Thank you, for the time you spend on my project.
    The the button is working know and next time I will use Dropbox for my sprites.
    You helped me alot for programming the gui.
    But is there a way to make a button which moves with the background?

  • dave1707dave1707 Mod
    Posts: 7,468

    @Gewis Sorry, I forgot about your question of a button that moves with the background. Here’s an example. Move the grid around the screen and tap one of the buttons.

    displayMode(FULLSCREEN)
    
    function setup()
        rectMode(CENTER)
        dx,dy=0,0
        str=""
    end
    
    function draw()
        background(0)
        stroke(255)
        strokeWidth(2)
        for x=0,2000,50 do
            line(dx+x,dy,dx+x,dy+2000)
        end
        for y=0,2000,50 do
            line(dx,dy+y,dx+2000,dy+y)
        end
        fill(255)
        rect(dx+400,dy+400,100,50)
        rect(dx+600,dy+600,100,50)
        fill(255,0,0)
        text("BUTTON 1",dx+400,dy+400)    
        text("BUTTON 2",dx+600,dy+600)   
        if str~="" then
            fill(255)
            rect(WIDTH/2,HEIGHT-50,200,50)
            fill(255,0,0)
            text(str,WIDTH/2,HEIGHT-50)
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            str=""
            if math.abs(t.x-(dx+400))<50 and math.abs(t.y-(dy+400))<25 then
                str="Button 1 pressed"
            end
            if math.abs(t.x-(dx+600))<50 and math.abs(t.y-(dy+600))<25 then
                str="Button 2 pressed"
            end
        end
        if t.state==MOVING then
            dx=dx+t.deltaX
            dy=dy+t.deltaY
        end
    end
    
  • edited September 2017 Posts: 9

    @dave1707
    I modified the code for my project. Now I am able to create moving and static buttons.

    Thank you!
    You make my wish come thourg to code my own game.
    Best regards from Germany.

Sign In or Register to comment.