Changing an image reference from other class

Monkey Forums/Monkey Programming/Changing an image reference from other class

Shinkiro1(Posted 2016) [#1]
Hi, I have an Animation class that should be able to alter a reference to an image (which is in a Sprite Class).

Strict
Import mojo2

Class MyApp Extends App
	Field sprite:Sprite
	Field canvas:Canvas
	Field animation:Animation
	
	Method OnCreate:Int()
		canvas = New Canvas
		sprite = New Sprite
		sprite.image = Image.Load("player.png")
		Local frames:= Image.LoadFrames("player_action.png", 3, False, 0, 0)
		animation = New Animation(sprite.image, frames)
		Return 0
	End
	
	Method OnUpdate:Int()
		animation.Update()
		Return 0
	End
	
	Method OnRender:Int()
		canvas.Clear()
		canvas.DrawImage(sprite.image, 100, 100)
		canvas.Flush()
		Return 0
	End
End

Class Sprite
	Field image:Image
End

Class Animation
	
	Method New(image:Image, frames:Image[])
		Self.image = image
		Self.frames = frames
	End
	
	Method Update:Void()
		time += 1
		time = time Mod 60
		If time > 40
			image = frames[2]
		Elseif time > 20
			image = frames[1]
		Else
			image = frames[0]
		End
	End
	
	Field image:Image
	Field frames:Image[]
	Field time:Int
End

Function Main:Int()
	New MyApp
	Return 0
End

The logic works, but the image reference stays the same in the sprite. Do I really have to to do sprite.image to change the reference?


Gerry Quinn(Posted 2016) [#2]
Why would you expect sprite.image to change? You changed animation.image to point to a different image, but sprite.image still points to what it pointed to originally. They aren't linked in any way.


Shinkiro1(Posted 2016) [#3]
Well, you are right.I don't know right know how I could have thought about it differently before.