Can't get ChangeObjectState to work

baardbi

Well-known member
On my game over screen (screen 255) I have a game object (10) that looks like the player. I want him to go from state 0 to 1 when I press start. I have assigned the script to the start button, so that works. But the object does not change state.

Here's the code that triggers on my game over screen when I press start:

And some screenshots after the code.

PS! I have created the OBJ_P1_GAMEOVER constant.


Code:
        ;; if on the GAME OVER screen
        LDA currentScreen
        CLC
        CMP #GAME_OVER_SCREEN
        BNE +
        
		StopSound
        
		;;CreateObject #$70, #$70, #$A, #$00, currentNametable
		LDX #OBJ_P1_GAMEOVER  ;;Game object nr 10 (with two animations)
		ChangeObjectState #$01, #$00
		
		PlaySound #SND_RESTARTGAME
        ;JMP RESET   ;;Go to Start screen

        +
        RTS ;; skip the code

Game object 10:

gameobject10.png


Actions game object 10:

actions0.png


actions1.png


Animations game object 10:

gameObj10animations.png


gameObj10animations2.png
 

dale_coop

Moderator
Staff member
You can't LDX your object like you did.
The objects created on screen have IDs from 0...N (they are instances of the Object types)
The player object is always created at first, so it's ID is 0... and when created his ID is stored in the player1_object variable.
Because we know the player is in player1_object (directly referred by that variable) we can get it, load it
But other objects on screen are not stored into fixed variable you could LOAD...

A solution would be to create by code your object on the screen and store it into a variable (after the createobject, you can call "STX myObjectPlayerGameOver")
Then you can "LDX myObjectPlayerGameOver" anytime, to do anything you want with that instance of the object.

Another possibility would be by doing to loop on all the objects on screen and if the Object_type of the object corresponds to #OBJ_P1_GAMEOVER, it's your object!
 

baardbi

Well-known member
I can't get it to work right now. But that's OK. I love learning like this, and getting a better understanding of how NESmaker works under the hood. I'm having a great time :)

Right now only the sound plays when I press start at the Game Over screen. Then nothing else happens. I wanted the Game Over character to be loaded on the Game Over screen, and when I press the start button I want him to change animation. Then I want the end of animation to trigger Restart Game.


Here's the code for my GameOverReset.asm (triggers when I press start at Game Over screen)

Code:
        ;; if on the GAME OVER screen
        LDA currentScreen
        CLC
        CMP #GAME_OVER_SCREEN
        BNE +
        
        StopSound
        
        LDX myObjectPlayerGameOver  ;;Game object nr 10 (with two animations)
        ChangeObjectState #$01, #$00
        
        PlaySound #SND_RESTARTGAME
        ;JMP RESET   ;;Go to Start screen

        +
        RTS ;; skip the code


Here's something I added at the end of WarpToScreen.asm:

Code:
		LDA currentScreen
        CLC
        CMP #GAME_OVER_SCREEN
        BNE +
        CreateObject #$64, #$64, #$A, #$00, currentNametable
		STX myObjectPlayerGameOver
		ChangeObjectState #$00, #$00
		;RTS ;; skip the code
        +
doneWithWarpToScreen:
 

dale_coop

Moderator
Staff member
It will not work because after your warp to screen script is executed, the game loop start again... every objects are destroyed (except the player), the new screen is loaded, the new objects for that screen are created.

So definitly, you should place this object on the "game over" screen, manually... and add a code searching for your object (with a loop on every objects) and set it in the variable before using it.
Maybe a code like that:

Code:
	;; if on the GAME OVER screen
	LDA currentScreen
	CLC
	CMP #GAME_OVER_SCREEN
	BEQ +
	RTS ;; skip the code
	+
	
	
	;; init variable 
	LDA #$FF
	STA myObjectPlayerGameOver	

	
	;; looping on every objects on the screen:	
	LDX #$00
-
	;; if the object is still active (not destroyed / out of screen):
	LDA Object_status,x
	AND #OBJECT_IS_ACTIVE
	BEQ +
	;; if the object is the one we are looking for:
	LDA Object_type,x
	CMP #OBJ_P1_GAMEOVER
	BNE +   
	;; yes it IS, we will keep it in a variable:
	STX myObjectPlayerGameOver	
	JMP ++
+
	INX
	CPX #TOTAL_MAX_OBJECTS
	BNE -
++
	;; search is over... now, we can use myObjectPlayerGameOver	(if not #$FF)

	LDX myObjectPlayerGameOver
	CPX #$FF
	BNE +
	RTS ;; skip the code
	+
	
	ChangeObjectState #$01, #$00
	
	PlaySound #SND_RESTARTGAME
	;JMP RESET   ;;Go to Start screen

	RTS ;; skip the code

(haven't tested it, I am at my office, byt the idea is here)
 
Top Bottom