I have been trying to learn f# over the past few weeks and am having a little trouble with certain aspects. I am trying to use it with XNA and am writing a very simple game.
I have a simple player class that implements DrawableGameComponent and then overrides its methods Draw, Update and LoadContent.
type Player (game:Game) =
inherit DrawableGameComponent(game)
let game = game
let mutable position = new Vector2( float32(0), float32(0) )
let mutable direction = 1
let mutable speed = -0.1
let mutable sprite:Texture2D = null
override this.LoadContent() =
sprite <- game.Content.Load<Texture2D>("Sprite")
override this.Update gt=
if direction = -1 && this.Coliding then
this.Bounce
this.Jumping
base.Update(gt)
override this.Draw gt=
let spriteBatch = new SpriteBatch(game.GraphicsDevice)
spriteBatch.Begin()
spriteBatch.Draw(sprite, position, Color.White)
spriteBatch.End()
base.Draw(gt)
and so on....
The main Game class then makes a new player object etc.
module Game=
type XnaGame() as this =
inherit Game()
do this.Content.RootDirectory <- "XnaGameContent"
let graphicsDeviceManager = new GraphicsDeviceManager(this)
let mutable player:Player = new Player(this)
let mutable spriteBatch : SpriteBatch = null
let mutable x = 0.f
let mutable y = 0.f
let mutable dx = 4.f
let mutable dy = 4.f
override game.Initialize() =
graphicsDeviceManager.GraphicsProfile <- GraphicsProfile.HiDef
graphicsDeviceManager.PreferredBackBufferWidth <- 640
graphicsDeviceManager.PreferredBackBufferHeight <- 480
graphicsDeviceManager.ApplyChanges()
spriteBatch <- new SpriteBatch(game.GraphicsDevice)
base.Initialize()
override game.LoadContent() =
player.LoadContent () //PROBLEM IS HERE!!!
this.Components.Add(player)
override game.Update gameTime =
player.Update gameTime
override game.Draw gameTime =
game.GraphicsDevice.Clear(Color.CornflowerBlue)
player.Draw gameTime
The compiler reports an error saying "Method or object constructor LoadContent not found"
I find this odd as both Draw and Update work ok and are found by intellisense but not LoadContent!
It is probably just a very silly error I have made but if anyone spots the problem I would be much obliged!
Thanks
DrawableGameComponent.LoadContent
is protected - so you don't have access to call it from your XnaGame
class.
It's not clear to me what's meant to end up calling it, but apparently you shouldn't be calling it directly yourself.
The error definitely sounds confusing. You're overriding the LoadContent
member in the definition of your Player
type, but (as Jon pointed out) the member is protected
. F# does not allow you to make the member more visible, so even your definition is still protected
(you cannot normally define protected
members in F#, so that's why the error message is poor).
You can solve the problem by adding additional member that calls LoadContent
from inside the Player
:
override this.LoadContent() =
sprite <- game.Content.Load<Texture2D>("Sprite")
member this.LoadContentPublic() =
this.LoadContent()
...then the LoadContent
member will still be protected
(inaccessible from the outside), but the new LoadContentPublic
member will be public (this is default for member
in F#) and you should be able to call it from your XnaGame
.
However, as Jon pointed out - maybe you shouldn't be calling the method yourself because the XNA runtime will call it automatically when it needs to.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.