can someone spot a problem???

Monkey Forums/Monkey Beginners/can someone spot a problem???

dubbsta(Posted 2017) [#1]
just started and already something ive done before giving me problems arghhh.

the mouse over is not functioning right it changes press frame on coords i didnt give it i dont understand.

in the mainscreen class under update please help.

strict

Import mojo

Class mouse

  Field image:Image

		Method New()
			image = LoadImage("PNG/UI/cursor.png")
		End
		
		Method Draw:Int()
			DrawImage(image,MouseX(),MouseY())
			Return 0
		End 
		
End 

Class MainScreen

  Field image:Image
  Field Sbutton:Image
  Field Obutton:Image
  Field SbuttX:Int = screenWidth/2
  Field SbuttY:Int = screenHeight/2
  Field ObuttX:Int = screenWidth/2
  Field ObuttY:Int = screenHeight/2
  Field Oframe:Int = 1 
  Field Sframe:Int = 1
   
  		Method New()
  			image = LoadImage("background1.png")
  			Sbutton = LoadImage("PNG/UI/start.png",380/2,49,2,Image.MidHandle)
  			Obutton = LoadImage("PNG/UI/options.png",380/2,49,2,Image.MidHandle)
  		End 
  		
  		Method Update:Int()
  			
  			If MouseX() <= SbuttX - 95 or  MouseX() >= SbuttX + 95  And MouseY() <= SbuttY - 24 Or MouseY() >= SbuttY + 24  
			 
  				Sframe = 0			  		 	 
  		 	Else		
  			 	Sframe = 1 
  			End  
  		 	
  			Return 0		 
  		End  
		
		Method Draw:Int()
		
			DrawImage(image,0,0)
			SetAlpha .9
			DrawImage(Sbutton,SbuttX,SbuttY+ 40,Sframe)
			DrawImage(Obutton,ObuttX,ObuttY+100,Oframe)	
			Return 0 
		End 
End 

Const screenWidth:Int = 640
Const screenHeight:Int = 480

Class spacegame Extends App 

  Field mainScreen:Int = 1
  Field options:Int = 2
  Field selectScreen:Int = 3
  Field gameOn:Int = 4
  Field gameOver:Int = 5
  Field gameState:int 
  
  'CLASSES
  Field splash:MainScreen
  Field pointer:mouse
  
  		Method OnCreate:Int()
  			SetUpdateRate(60)
  			
  			'HideMouse()
  			gameState = mainScreen
  			splash = New MainScreen
  			pointer = New mouse
  			Return 0 
  		End 
  		
  		Method OnUpdate:Int()
  			
  			Select gameState
  				
  				Case mainScreen
  					splash.Update()
  				Case options
  				
  				Case selectScreen
  				
  				Case gameOn
  				
  				Case gameOver
  				
  			End 
  		
  			Return 0 
  		End 
  		
  		Method OnRender:Int()
  			Cls(0,0,0)
  			
  			Select gameState
  				
  				Case mainScreen
  					
  					splash.Draw()
  					pointer.Draw()
  				Case options
  				
  				Case selectScreen
  				
  				Case gameOn
  				
  				Case gameOver
  				
  			End 
  			
  			Return 0
  		End 
  		
End 

Function Main:Int()

	New spacegame()

	Return 0 
End 



Gerry Quinn(Posted 2017) [#2]
If MouseX() <= SbuttX - 95 or MouseX() >= SbuttX + 95 And MouseY() <= SbuttY - 24 Or MouseY() >= SbuttY + 24

I think your logic is wrong (And should be Or) , Any one of those four equations being true means the mouse is outside the central button area.

Also, And has higher priority than Or, so you would get weird results even if the equations were what you intended. Use brackets always, unless operator priority is second nature to you.


Jesse(Posted 2017) [#3]
it should be like this:
If (MouseX() <= SbuttX - 95 or  MouseX() >= SbuttX + 95)  And (MouseY() <= SbuttY - 24 Or MouseY() >= SbuttY + 24)

this tests to see if the mouse is outside the sbutt area.

it can also be with an Or as Garry said:
If MouseX() <= SbuttX - 95 or  MouseX() >= SbuttX + 95  Or MouseY() <= SbuttY - 24 Or MouseY() >= SbuttY + 24

if anyone of those test are true the mouse is outside Sbutt area.


dubbsta(Posted 2017) [#4]
thanks guys seems right but still not working...


Jesse(Posted 2017) [#5]
My mistake, didn't think it trough thoroughly.
you also need to take into consideration your offset of 40 to where you are drawing the button.

Here:



I recommend you create a button class. will make it easier to manage.


Jesse(Posted 2017) [#6]
.


dubbsta(Posted 2017) [#7]
Ahhh was thinking that thought there was some offset issue but wasn't sure, should've tried. But figured just needed the x location and image dimensions and the offset was already included in the calculations... Will try later and post back thx


dubbsta(Posted 2017) [#8]
perfect added the offset to both y's works great another lesson learned thanks gents!!!