Mouse trouble

Game development specific discussions.
ITomi
Posts: 121
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: Mouse trouble

Postby ITomi » Aug 17, 2019 8:18

Yes, BasicCoder2, my problem is exactly that you explain your code.
I thought, something wrong with the FOR loop in my program somewhere here:

Code: Select all

(...) elseif leftmousebuttonpressed=1 and mousex>=scrollarrows(i).xplace and mousex<=scrollarrows(i).xplace+64 and mousey>=scrollarrows(i).yplace and mousey<=scrollarrows(i).yplace+64 then
                        if i=0 then
                            if arraystart>0 and canscrollmenu=0 then
                                arraystart-=1
                                for j as ubyte=0 to numofshowicon-1
                                    if showicons(j).exists=1 then showicons(j).itssprite=iconarray(arraystart+j)
                                next j
                                canscrollmenu=10
                            end if
                            scrarrsprite=leftarrowspr(0)
                        else
                            if arraystart+(numofshowicon-1)<numoficons-1 and canscrollmenu=0 then
                                arraystart+=1
                                for j as ubyte=0 to numofshowicon-1
                                    if showicons(j).exists=1 then showicons(j).itssprite=iconarray(arraystart+j)
                                next j
                                canscrollmenu=10
                            end if
                            scrarrsprite=rightarrowspr(0)
                        end if
                        leftmousebuttonpressed=0
                    end if

which not execute the 0 expression, but maybe I organize my code badly...
BasicCoder2
Posts: 3397
Joined: Jan 01, 2009 7:03

Re: Mouse trouble

Postby BasicCoder2 » Aug 17, 2019 9:56

@ITomi,
I did try to figure out where the code wasn't working as you expected but I am not very good at unravelling other people's code instead I find it easier to just write my own version based on the behaviors required. I have added code to the previous post to change the color of the scroll buttons when the mouse is over them.
dodicat
Posts: 5913
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Mouse trouble

Postby dodicat » Aug 17, 2019 9:58

Try this.
1) Put the variables
dim as integer mousex,mousey,mousebutton,mouse
at the beginning of the sub so they are global to the goings on.
2) Just before leaving the sub, put
leftmousebuttonpressed=mousebutton

Code: Select all

 #include "fbgfx.bi"
Using FB

dim shared as ubyte numofshowicon,leftmousebuttonpressed=0,arraystart,canscrollmenu
dim shared as any ptr whitecircle,leftarrowspr(2),rightarrowspr(2)

type icons
    as ushort xplace,yplace
    as ubyte exists,pressed
    as any ptr itssprite
end type

dim shared numoficons as ubyte=0
redim shared showicons(numoficons) as icons
redim shared scrollarrows(2) as icons
dim shared iconarray(9) as any ptr

screenres 640,480,32

function wcr(n as ubyte) as any ptr
    whitecircle=imagecreate(64,64)
    circle whitecircle,(32,32),28,Color RGB(0, 0, 0)
    draw string whitecircle,(1,1),str(n)
    return whitecircle
end function

leftarrowspr(0)=imagecreate(64,64)
circle leftarrowspr(0),(32,32),32,Color RGB(255, 0, 0)
draw string leftarrowspr(0),(1,1),"<-"
leftarrowspr(1)=imagecreate(64,64)
circle leftarrowspr(1),(32,32),32,Color RGB(0, 0, 255)
draw string leftarrowspr(1),(1,1),"<-"
rightarrowspr(0)=imagecreate(64,64)
circle rightarrowspr(0),(32,32),32,Color RGB(255, 0, 0)
draw string rightarrowspr(0),(1,1),"->"
rightarrowspr(1)=imagecreate(64,64)
circle rightarrowspr(1),(32,32),32,Color RGB(0, 0, 255)
draw string rightarrowspr(1),(1,1),"->"

function min(num1 as double, num2 as double) as double
    if num1<num2 then
        return num1
    else
        return num2
    end if
end function

sub showtheicons()
    dim as integer mousex,mousey,mousebutton,mouse
    for i as ubyte=0 to 1
        if scrollarrows(i).exists=1 then
           ' dim as integer mousex,mousey,mousebutton,mouse
            dim as any ptr scrarrsprite=scrollarrows(i).itssprite
            mouse=getmouse(mousex,mousey,,mousebutton)
            if mouse=0 then
                if (mousebutton and 1) then
                    if mousex>=scrollarrows(i).xplace and mousex<=scrollarrows(i).xplace+64 and mousey>=scrollarrows(i).yplace and mousey<=scrollarrows(i).yplace+64 then
                        select case scrollarrows(i).itssprite
                        case leftarrowspr(0)
                            scrarrsprite=leftarrowspr(1)
                        case rightarrowspr(0)
                            scrarrsprite=rightarrowspr(1)
                        end select
                        leftmousebuttonpressed=1
                    else
                        select case scrollarrows(i).itssprite
                        case leftarrowspr(1)
                            scrarrsprite=leftarrowspr(0)
                        case rightarrowspr(1)
                            scrarrsprite=rightarrowspr(0)
                        end select
                        'leftmousebuttonpressed=0
                    end if
                elseif leftmousebuttonpressed=1 and mousex>=scrollarrows(i).xplace and mousex<=scrollarrows(i).xplace+64 and mousey>=scrollarrows(i).yplace and mousey<=scrollarrows(i).yplace+64 then
               
                   
                        if i=0 then
                            if arraystart>0 and canscrollmenu=0 then
                                arraystart-=1
                                for j as ubyte=0 to numofshowicon-1
                                    if showicons(j).exists=1 then showicons(j).itssprite=iconarray(arraystart+j)
                                next j
                                canscrollmenu=10
                            end if
                            scrarrsprite=leftarrowspr(0)
                        else
                            if arraystart+(numofshowicon-1)<numoficons-1 and canscrollmenu=0 then
                                arraystart+=1
                                for j as ubyte=0 to numofshowicon-1
                                    if showicons(j).exists=1 then showicons(j).itssprite=iconarray(arraystart+j)
                                next j
                                canscrollmenu=10
                            end if
                            scrarrsprite=rightarrowspr(0)
                        end if
                        'leftmousebuttonpressed=0
                    end if
            end if
           
            put (scrollarrows(i).xplace, scrollarrows(i).yplace),scrarrsprite,pset
        end if
    next i
    for i as ubyte=0 to min(2,numoficons-1)
        if showicons(i).exists=1 then
            put (showicons(i).xplace, showicons(i).yplace),showicons(i).itssprite,pset
        end if
    next i
    leftmousebuttonpressed=mousebutton
end sub

iconarray(numoficons)=wcr(numoficons) : numoficons+=1
iconarray(numoficons)=wcr(numoficons) : numoficons+=1
iconarray(numoficons)=wcr(numoficons) : numoficons+=1
iconarray(numoficons)=wcr(numoficons) : numoficons+=1
iconarray(numoficons)=wcr(numoficons) : numoficons+=1
iconarray(numoficons)=wcr(numoficons) : numoficons+=1
iconarray(numoficons)=wcr(numoficons) : numoficons+=1
iconarray(numoficons)=wcr(numoficons) : numoficons+=1
iconarray(numoficons)=wcr(numoficons) : numoficons+=1
dim as ushort xh=4
redim preserve scrollarrows(0)
scrollarrows(0).exists=1
scrollarrows(0).xplace=xh : xh+=64 : scrollarrows(0).yplace=40
scrollarrows(0).itssprite=leftarrowspr(0)
numofshowicon=0 : arraystart=0
for i as ubyte=0 to min(2,numoficons-1)
        redim preserve showicons(i)
        showicons(i).exists=1 : numofshowicon+=1
        showicons(i).xplace=xh : xh+=64 : showicons(i).yplace=40
        showicons(i).itssprite=iconarray(i) : showicons(i).pressed=0
next i
redim preserve scrollarrows(1)
scrollarrows(1).exists=1
scrollarrows(1).xplace=xh : scrollarrows(1).yplace=40
scrollarrows(1).itssprite=rightarrowspr(0)

do
            screenlock
            cls
            showtheicons()
            if canscrollmenu>0 then canscrollmenu-=1
            screenunlock
            sleep 50
loop until multikey(sc_q)
imagedestroy whitecircle
for i as ubyte=0 to 1
    imagedestroy leftarrowspr(i)
    imagedestroy rightarrowspr(i)
next i 

This replicates basiccoder2's code behaviour.
ITomi
Posts: 121
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: Mouse trouble

Postby ITomi » Aug 17, 2019 10:02

Here is the key to my problem:

Code: Select all

if (mousebutton and 1) then
                    if mousex>=scrollarrows(i).xplace and mousex<=scrollarrows(i).xplace+64 and mousey>=scrollarrows(i).yplace and mousey<=scrollarrows(i).yplace+64 then
                        select case scrollarrows(i).itssprite
                        case leftarrowspr(0)
                            scrarrsprite=leftarrowspr(1)
                        case rightarrowspr(0)
                            scrarrsprite=rightarrowspr(1)
                        end select
                        leftmousebuttonpressed=1
                    else
                        select case scrollarrows(i).itssprite
                        case leftarrowspr(1)
                            scrarrsprite=leftarrowspr(0)
                            [b]leftmousebuttonpressed=0[/b] //GOOD PLACE!
                        case rightarrowspr(1)
                            scrarrsprite=rightarrowspr(0)
                            [b]leftmousebuttonpressed=0[/b] //GOOD PLACE!
                        end select
                        ' leftmousebuttonpressed=0 //WRONG PLACE!!!
                    end if
                elseif leftmousebuttonpressed=1(...)

The leftmousebuttonpressed=0 was on wrong place and therefore it always reset to 0 this value so the code couldn't run the ELSEIF when i was 0.
dodicat
Posts: 5913
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Mouse trouble

Postby dodicat » Aug 17, 2019 10:06

ITomi
Didn't you try my change to your code?
ITomi
Posts: 121
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: Mouse trouble

Postby ITomi » Aug 17, 2019 12:58

dodicat wrote:ITomi
Didn't you try my change to your code?


Not yet, but I begin to make simplier the code and I will use your change too.
Thank you for everybody!
BasicCoder2
Posts: 3397
Joined: Jan 01, 2009 7:03

Re: Mouse trouble

Postby BasicCoder2 » Aug 17, 2019 21:17

dodicat wrote:Try this.
1) Put the variables
dim as integer mousex,mousey,mousebutton,mouse
at the beginning of the sub so they are global to the goings on.
2) Just before leaving the sub, put
leftmousebuttonpressed=mousebutton
...
This replicates basiccoder2's code behaviour.


Well worked out dodicat :)

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 3 guests