Matrix overlapping

Monkey Forums/Monkey Beginners/Matrix overlapping

Moerin(Posted 2014) [#1]
Hello there.

I encounter a problem with the Matrix use.

I need to make a card shake so, i use PushMatrix and PopMatrix in this way.
Strict

Import diddy

Function Main:Int()
	New Game()
	Return 0
End

Global gameScreen:GameScreen

Class Game Extends DiddyApp
	
	Method Create:Void()
		' diddy millisecs version
		Seed = RealMillisecs()
		gameScreen = New GameScreen
		gameScreen.PreStart()

	End
		
End Class

Class GameScreen Extends Screen
	' State of game
	Field _cardCard:Card
	Field _stateCard:Int
	
	Method New()
		name = "Game"
	End Method
	
	Method Start:Void()
		_cardCard = New Card(SCREEN_WIDTH2, SCREEN_HEIGHT2)
	End Method
		
	Method Update:Void()
		If KeyHit(KEY_1) Then
			_stateCard = 1
		ElseIf KeyHit(KEY_2) Then
			_stateCard = 2
		ElseIf KeyHit(KEY_3) Then
			_stateCard = 3
		ElseIf KeyHit(KEY_4) Then
			_stateCard = 4
		EndIf
		_cardCard.Update()
	End Method
		
	Method Render:Void()
		Cls
		DrawText "Key 1 = HIDDEN CARD, KEY 2 = REVEALED CARD, KEY 3 = SHOWING CARD", SCREEN_WIDTH2, 10, 0.5, 0.5
		_cardCard.Draw(_stateCard)
	End Method
	
End Class

Class Card
	Const HIDDEN:Int = 1
	Const REVEALED:Int = 2
	Const SHOWING:Int = 3
	Const SHAKING:Int = 4
	
	' Image Field
	Field _gfxCard:Image
	Field _nFrameOffset:Int
	
	' Coordinate	
	Field _fXpos:float, _fYpos:Float
	
	' Flip Angle
	Field _fScale:Float = -1.0

	Method New(_pfXpos:Float, _pfYpos:Float)
		_gfxCard = LoadImage("spriteSheet.png", 120, 166, 2, Image.MidHandle)
		_fXpos = _pfXpos
		_fYpos = _pfYpos
	End Method
	
	Method Update:Void()
		_fScale += 0.1
	End Method
	
	Method GetWidth:Float()
		Return _gfxCard.Width()
	End Method
	
	Method GetHeight:Float()
		Return _gfxCard.Height()
	End Method
	
	Method GetXPos:Float()
		Return _fXpos
	End Method
	
	Method GetYPos:Float()
		Return _fYpos
	End Method
	
	Method Draw:Void(pCurrentState:int)
		Local frame:int
		Local scaleX:Float = 1
		
		If pCurrentState = HIDDEN Then
			frame = 1
		ElseIf pCurrentState = REVEALED Then
			frame = 0
		ElseIf pCurrentState = SHOWING Then
			scaleX = Sinr(_fScale)
			If scaleX < 0 Then
				frame = 1
			Else
				frame = 0
			EndIf
		ElseIf pCurrentState = SHAKING Then
			PushMatrix
				Translate _fXpos, _fYpos
				PushMatrix
					DrawImage(_gfxCard, 0, 0, Sinr(_fScale), 1.0, 1.0, frame)
				PopMatrix
			PopMatrix
		EndIf
		DrawImage(_gfxCard, _fXpos, _fYpos, 1.0, scaleX, 1.0, frame)
	End Method
	
End Class


The problem that is that when i press on the to shake the card instead of appears alone the card overlap the old one.

Where am I wrong?


Jesse(Posted 2014) [#2]
you are drawing it shaking when shaking and otherwise. You are drawing the card in the last line of the Draw Method(always) and in the else if of the shaking option.


Moerin(Posted 2014) [#3]
Ok i thought then when you use PushMatrix all the elements not surrounded by Push and Pop disappear and reappear after the Pop.

Ok i have changed my code and now it's work fine.

Thanks.


NoOdle(Posted 2014) [#4]
You also don't need that many Push and Pops. PushMatrix will push the current transformation onto a stack so that you can get it back when you are done. So in your example you only need:

[bbcode]
PushMatrix
Translate _fXpos, _fYpos
DrawImage(_gfxCard, 0, 0, Sinr(_fScale), 1.0, 1.0, frame)
PopMatrix
[/bbcode]