Method Draw Cannot be accessed from there?

Monkey Forums/Monkey Programming/Method Draw Cannot be accessed from there?

Hotshot(Posted 2011) [#1]
I have been using his Diddy Framework and trying to make simple pong game as I try draw ball on screen when testing ball that bounce round the screen.

The Background, music and sound will be change when come to finishing the pong game :)


' Strict code mode please
Strict

' The modules
Import diddy

' Starting Point
Function Main:Int()
	game = New MyGame()
End

' Screens in the game
Global titleScreen:Screen = New TitleScreen()
Global gameScreen:Screen = New GameScreen()


' The Game
Class MyGame Extends DiddyApp
	Method OnCreate:Int()
		Super.OnCreate()
		
		' Set the font
		SetFont(LoadImage("graphics/font_16.png",16,16,64))
		
		LoadImages()
		LoadSounds()
		
		titleScreen.PreStart()
		
		Return 0
	End
	
	'***********************
	'* Load Images
	'***********************
	Method LoadImages:Void()
		' create tmpImage for animations
		Local tmpImage:Image
		
		images.Load("galaxy2.png", "", False)
		images.Load("BAT.png","",False)
		images.Load("Ball.png","",False)
					
	End
	
	'***********************
	'* Load Sounds
	'***********************
	Method LoadSounds:Void()
		'sounds.Load("lazer")
		'sounds.Load("boom3")
	End
End

Class TitleScreen Extends Screen
	Field background:GameImage
	
	Method New()
		name = "Title"
	End

	Method Start:Void()
		background = game.images.Find("galaxy2")
		'game.MusicPlay("ShowYourMoves.ogg", True)
		game.screenFade.Start(50, False)
	End
	
	Method Render:Void()
		DrawImage background.image, 0, 0
		Scale 2, 2
		DrawText "P O N G", SCREEN_WIDTH2 / 2, (SCREEN_HEIGHT2-30) / 2, 0.5, 0.5
		Scale .5, .5
		DrawText "SPACE TO PLAY!", SCREEN_WIDTH2, SCREEN_HEIGHT2 + 60, 0.5, 0.5
	End

	Method Update:Void()
		If KeyHit(KEY_SPACE) Or MouseHit(0)
			game.screenFade.Start(50, True, True, True)
			game.nextScreen = gameScreen
		End
		If KeyHit(KEY_ESCAPE)
			game.screenFade.Start(50, True, True, True)
			game.nextScreen = game.exitScreen
		End
	End
End

Class GameScreen Extends Screen
	Field background:GameImage
	Field player:Player
	Field ball:Ball
	Field lifeImage:GameImage
			
	Method New()
		name = "Game Screen"
	End

	Method Start:Void()
		background = game.images.Find("galaxy2")
		Local gi:GameImage = game.images.Find("Bat")
		Local gi2:GameImage = game.images.Find("Ball")
		
		player = New Player(gi, SCREEN_WIDTH2, SCREEN_HEIGHT - gi.h)
		ball = New Ball(gi2, 10, 10)

		StartLevel()
		'game.MusicPlay("SpaceFighterLoop.ogg", True)
		'start fade
		game.screenFade.Start(50, False, True, True)
	End
	
	
	Method StartLevel:Void(level% = 1)
	   Local B_SpeedX:Int,B_SpeedY:Int
		
		B_SpeedX=1
		B_SpeedY=1

	End
	
	Method Render:Void()
		DrawImage background.image, 0, 0
		player.Draw()
		Ball.Draw()

		DrawGUI()
	End
	
	Method DrawGUI:Void()
		DrawText "SCORE: "+ player.score, 0, 0
	End
	

	Method Update:Void()
		player.Update()
		Ball.Update()


		If KeyHit(KEY_ESCAPE)
			game.screenFade.Start(50, True, True, True)
			game.nextScreen = titleScreen
		End
	End
End
	
Class Ball Extends Sprite
      Field X:Int
      Field Y:Int
      Field B_SpeedX:Int
      Field B_SpeedY:Int

     Method New(img:GameImage, x#, y#)
		Super.New(img, x, y)
		X = 50
		Y = 50
	End
	
	Method Update:Void()
    
    End
End

Class Player Extends Sprite
	Field score:Int
	Field lives:Int
		
	Method New(img:GameImage, x#, y#)
		Super.New(img, x, y)
		score = 0
		lives = 3
		speedX = 1
		maxXSpeed = 5
	End
	
	Method Update:Void()
		If KeyDown(KEY_LEFT)
			Self.dx-=Self.speedX
	
		Else If KeyDown(KEY_RIGHT)
			Self.dx+=Self.speedX			
		End
		
		If dx > Self.maxXSpeed
			dx = Self.maxXSpeed
		End
		
		If dx < -Self.maxXSpeed
			dx = -Self.maxXSpeed
		End
		Self.Move()
		
		' limit the player to the screen
		If x <= -25 
			x = -25
		End
		
		If x > 440
		   x = 440
		End
	End
					
End



Volker(Posted 2011) [#2]
It's because the image field in GameImage is set to private.
Wondering about this already. Came to the conclusion that
the guys have something to hide... Will better wear my aluminum cap tonight.


Hotshot(Posted 2011) [#3]
how do I set Gameimage is not set to private?


the guys have something to hide... Will better wear my aluminum cap tonight.



EH!?


therevills(Posted 2011) [#4]
@Hotshot, try this:



The issue is that you were accessing Ball not ball... remember Monkey is case sensitive.

Method Render:Void()
		DrawImage background.image, 0, 0
		player.Draw()
		ball.Draw() '<----- 

		DrawGUI()
	End


Monkey spat the error, because it is not static.

@Volker - EH!?


Hotshot(Posted 2011) [#5]

The issue is that you were accessing Ball not ball... remember Monkey is case sensitive.



I see....

thanks therevills :)


Hotshot(Posted 2011) [#6]
I cant seem to move the ball....do you know why?


' Strict code mode please
Strict

' The modules
Import diddy

' Starting Point
Function Main:Int()
	game = New MyGame()
End

' Screens in the game
Global titleScreen:Screen = New TitleScreen()
Global gameScreen:Screen = New GameScreen()

Global X:Int,Y:Int
Global B_SpeedX:Int,B_SpeedY:Int


' The Game
Class MyGame Extends DiddyApp
	Method OnCreate:Int()
		Super.OnCreate()
		
		' Set the font
		SetFont(LoadImage("graphics/font_16.png",16,16,64))
		
		LoadImages()
		LoadSounds()
		
		titleScreen.PreStart()
		
		Return 0
	End
	
	'***********************
	'* Load Images
	'***********************
	Method LoadImages:Void()
		' create tmpImage for animations
		Local tmpImage:Image
		
		images.Load("galaxy2.png", "", False)
		images.Load("BAT.png","",False)
		images.Load("BALL.png","",False)
					
	End
	
	'***********************
	'* Load Sounds
	'***********************
	Method LoadSounds:Void()
		'sounds.Load("lazer")
		'sounds.Load("boom3")
	End
End

Class TitleScreen Extends Screen
	Field background:GameImage
	
	Method New()
		name = "Title"
	End

	Method Start:Void()
		background = game.images.Find("galaxy2")
		'game.MusicPlay("ShowYourMoves.ogg", True)
		game.screenFade.Start(50, False)				
	End
	
	Method Render:Void()
		DrawImage background.image, 0, 0
		Scale 2, 2
		DrawText "P O N G", SCREEN_WIDTH2 / 2, (SCREEN_HEIGHT2-30) / 2, 0.5, 0.5
		Scale .5, .5
		DrawText "SPACE TO PLAY!", SCREEN_WIDTH2, SCREEN_HEIGHT2 + 60, 0.5, 0.5
	End

	Method Update:Void()
		If KeyHit(KEY_SPACE) Or MouseHit(0)
			game.screenFade.Start(50, True, True, True)
			game.nextScreen = gameScreen
		End
		If KeyHit(KEY_ESCAPE)
			game.screenFade.Start(50, True, True, True)
			game.nextScreen = game.exitScreen
		End
	End
End

Class GameScreen Extends Screen
	Field background:GameImage
	Field player:Player
	Field ball:Ball
	Field lifeImage:GameImage
			
	Method New()
		name = "Game Screen"
	End

	Method Start:Void()
		background = game.images.Find("galaxy2")
		Local gi:GameImage = game.images.Find("BAT")
		Local gi2:GameImage = game.images.Find("BALL")
		
		player = New Player(gi, SCREEN_WIDTH2, SCREEN_HEIGHT - gi.h)
		ball = New Ball(gi2, 10, 10)

		StartLevel()
		'game.MusicPlay("SpaceFighterLoop.ogg", True)
		'start fade
		game.screenFade.Start(50, False, True, True)		
	End
	
	
	Method StartLevel:Void(level% = 1)	   
		
		B_SpeedX=1
		B_SpeedY=1

	End
	
	Method Render:Void()
		DrawImage background.image, 0, 0
		player.Draw()
		ball.Draw()

		DrawGUI()
	End
	
	Method DrawGUI:Void()
		DrawText "SCORE: "+ player.score, 0, 0
	End
	

	Method Update:Void()

		player.Update()
		ball.Update()
		        
		If KeyHit(KEY_ESCAPE)
			game.screenFade.Start(50, True, True, True)
			game.nextScreen = titleScreen
		End
	End
End
	
Class Ball Extends Sprite
      Field X:Int
      Field Y:Int
      Field B_SpeedX:Int
      Field B_SpeedY:Int

     Method New(img:GameImage, x#, y#)
		Super.New(img, x, y)
		X = 50
		Y = 50
	End
	
	Method Update:Void()
        X=X+B_SpeedX
        Y=Y+B_SpeedY

        ' IF THE BALL HIT ON RIGHT SIDE THEN LET BOUNCE TO THE LEFT :)        
        If X>640 
           B_SpeedX=-1        
        Endif

        'IF THE BALL HIT ON LEFT SIDE THEN LET BOUNCE TO THE RIGHT
        If X<0
           B_SpeedX=+1
        Endif
    End
End

Class Player Extends Sprite
	Field score:Int
	Field lives:Int
		
	Method New(img:GameImage, x#, y#)
		Super.New(img, x, y)
		score = 0
		lives = 3
		speedX = 1
		maxXSpeed = 5
	End
	
	Method Update:Void()
		If KeyDown(KEY_LEFT)
			Self.dx-=Self.speedX
	
		Else If KeyDown(KEY_RIGHT)
			Self.dx+=Self.speedX			
		End
		
		If dx > Self.maxXSpeed
			dx = Self.maxXSpeed
		End
		
		If dx < -Self.maxXSpeed
			dx = -Self.maxXSpeed
		End
		Self.Move()
		
		' limit the player to the screen
		'****************************************
		If x <= -25 
			x = -25
		End
		
		If x > 440
		   x = 440
		End
    	'****************************************
	End					
End


What is the Best IDE for Monkey?


muddy_shoes(Posted 2011) [#7]
You've defined B_SpeedX and Y both as fields of the ball and as globals. You're setting the globals and reading the fields.


Volker(Posted 2011) [#8]
Sorry guys, looks like I'm disorganized.
But I could swear, that I once tried to access the GameImage.image.width(),
which failed because image was set to private. Curious.

[quote]EH!? [quote]
Just joking. Failed. Forget about this.


therevills(Posted 2011) [#9]
@Hotshot - you are creating your own X and Y, the sprites have their own x and y which it uses to draw the images. Also in sprite there is dx and dy so you dont need B_SpeedX or B_SpeedY. Also can you wrap your code in a codebox please?

@Volker - strange...