Physics gravity and pausing

Hi, recently, I’ve been having a pair of issues with Cocos Creator. First, gravity is incredibly slow when the player falls after coming in contact with the top of an object. In the past, I’ve been using cc.director.getPhysicsManager().gravity = cc.v2 (); to override the gravity issue, however, this was only a temporary fix (other non-player things are also affected) and I am unsure what is causing this.

Secondly, the cc.director.resume function is not working properly after many attempts to get it to work. The second issue generates a bunch of error messages to the browser console, namely the messages “The thing you want to instantiate is nil”, “TypeError: child is null”, and “TypeError: notifBox is undefined”, even when the correct resource has been dropped into the provided slot in Cocos Creator. I have posted the code below.

Thanks!

cc.Class({
extends: cc.Component,

properties: {
notifBoxPrefab: {
        default: null,
        type: cc.Prefab
    },
okPrefab: {
	default: null,
	type: cc.Prefab
},		
backPrefab: {
	default: null,
	type: cc.Prefab
},
textButtonPrefab: {
	default: null,
	type: cc.Prefab
},
textLabel: {
	default: null,
	type: cc.Label
},
},
// LIFE-CYCLE CALLBACKS:

onLoad: function () {},

loadLvlSelect: function (event) {
	cc.director.loadScene('Lvl Select');
	cc.director.preloadScene('Lvl One');
	cc.director.preloadScene('Lvl Two');
	cc.director.preloadScene('Lvl Three');
	cc.director.preloadScene('Lvl Four');
	cc.director.preloadScene('Test Lvl');
	cc.director.preloadScene('P-A-L');
	cc.director.preloadScene('Misc');
},

loadLvlOne: function (event) {
    cc.director.loadScene('Lvl One');
},

loadLvlTwo: function (event) {
    cc.director.loadScene('Lvl Two');
},	 

loadLvlThree: function (event) {
    cc.director.loadScene('Lvl Three');
},

loadLvlFour: function (event) {
    cc.director.loadScene('Lvl Four');
},	  

backToMain: function (event) {
    cc.director.loadScene('Main Menu');
},

loadTestLvl: function (event) {
    cc.director.loadScene('Test Lvl');
},

loadProjectiles: function (event) {
    cc.director.loadScene('P-A-L');
},

loadMisc: function (event) {
    cc.director.loadScene('Misc');
},

showNotifBox: function (event) {
 	var newNotif = cc.instantiate(this.notifBoxPrefab);
	this.node.zIndex = 1;
	this.node.addChild(newNotif);
	newNotif.setPosition(cc.v2(20, 240));
	
	var backButton = cc.instantiate(this.backPrefab);
	this.node.zIndex = 2;
	this.node.addChild(backButton);
	backButton.setPosition(cc.v2(-265, 445));
},

closeNotifBox: function (event) {
	this.node.destroy(this.notifBoxPrefab);
	this.node.destroy(this.backPrefab);
},

pauseGame: function () {
	cc.director.pause();
	var newNotif = cc.instantiate(this.notifBoxPrefab);
	this.node.zIndex = 1;
	this.node.addChild(newNotif);
	newNotif.setPosition(cc.v2(415, -260));
	
	var newOK = cc.instantiate(this.okPrefab);
	this.node.zIndex = 2;
	this.node.addChild(newOK);
	newOK.setPosition(cc.v2(605, -370));
	
	//This button keeps relocating the other two prefabs above it
	var newTButton = cc.instantiate(this.textButtonPrefab);
	this.node.zIndex = 2;
	this.node.addChild(newTButton);
	newNotif.setPosition(cc.v2(415, -260));
},

runGame: function () {
	var notifBox = notifBox.node.name === "newNotif";
          //have also tried "var notifBox = this.notifBoxPrefab;"
	if (notifBox.isValid == true) {
		this.node.destroy(notifBox);
		cc.director.resume();
	}
},

// update (dt) {}
});

Hi,Thanks for you feedback,whitch cocoscreator version you use? can you just get me simple demo to confirm this bug?

the code you have in runGame is strange :slight_smile:

var notifBox = notifBox.node.name === "newNotif"; in this case, notifBox is a boolean and the check should simply be:

if (notifBox) {
// do something
}

if you choose to do var notifBox = this.notifBoxPrefab;, then the check should be:

if (cc.isValid(notifBox))
{
  // do something
}
1 Like

Thanks for the help! The resume function now works, but the code doesn’t seem to understand what nodes to destroy. (The pauseGame function creates a notification box, and a button which resumes the game, but upon resuming with runGame, only the button is destroyed) Declaring variables in onLoad and using the return keyword don’t fix the issue. Do you know how to fix this as well?

i think it would be more efficient to create 2 separate prefabs, one for pause game and one for generic notification, with all the buttons included inside. this way, you won’t have to manually create those buttons (which is prone to error and you’re kind of missing all the visual feeling). also, when you are destroying the notification prefab, everything related will be destroyed with it. right now, it seems that the notification instance is destroyed, but the buttons are staying.

Thanks, Karg :slight_smile:

On the other issue, I use version 1.9 of Cocos Creator. Here’s a sample project:

ErrorExample.zip (275.0 KB)

When the “Player” rigidBody sets the gravity scale property to 1, it will drop slowly. This is normal. Setting the gravity scale to 0 can solve this problem. And you can use the action to simulate the falling animation of the object, which can achieve the same effect.
Regarding the second question, I think the message below shows that notifBox is wrong. The problem may be that it is no longer available. Use cc.isValid () to determine if the object is available and check your code for errors.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.