What's wrong with this code......?

Hi,

I’m relative new to Cocos2Dx and Cocos Creator, and trying to get a grasp of the code, would appreciate any feedback regarding the below code:

var rand = Math.round(random(min-350, max+360));

this.node.setPosition(this.duckposx, this.rand);

I’m trying to set Position Y at random between min, max for Sprite…Would appreciate any feedback…God Bless…

Sincerely,

Sunday

what are min and max?

Hi Slackmoehrle,

They are min Y Set Position and max Set Positions.…Thanks for quick reply…God Bless…

Sincerely,

Sunday

but what data type and value are they when you do the addition and subtraction?

Hi there,

What I’m trying to accomplish is to Set Position Y randomly between min = -350, and max =360… Sorry, but I’m coming from a drag and drop logic game engine, and I would just plug in rand ( -350, 360 ) into self position y and it would work…God Bless…

Sincerely,

Sunday

I think you can just do:

var rand = Math.random(-350, 360);

Here is the Mozilla doc

I’m not sure why you would Math.round() if you are dealing with whole numbers.

Hi,

Please see whole code:

cc.Class({
    extends: cc.Component,

    properties: {
        duckposx:500,
       
    },

    // use this for initialization
    onLoad: function () {
var rand = Math.random(-350, 360);
this.node.setPosition(this.duckposx, this.rand); 
// Play Duck Fly Anim Clip
var duckflyleft = this.node.getComponent(cc.Animation);
duckflyleft.play("duckflyleftanim");
// Move Left Random 
var duckmoveleftx = new cc.MoveBy(2, cc._p(-1078,0));
// Action Duck 
this.node.runAction(duckmoveleftx);

    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});

However, still doesn’t work, the duck doesn’t come out in the screen…
Thanks for the help and link, much appreciation…God Bless…

Sincerely,

Sunday

Well -350 is off screen for sure, anything to not positive is.

Hi,

Yes, but I’m getting the number from the node position of the sprite = duck.

sure, but what if the Y position of duck is 100 and then you get a random value of -250. You are off screen.

change your range to something smaller to get it working and debug.

Hi,

Yes, but that’s what I’m looking for, in essence I have my duck sprite off screen, regarding X and Y position, but then I’m just looking to spawn it in the random Y Position off screen in a function at time / schedule intervals, it will start off screen then use the cc.MoveBy action across the screen, with a collider attached then it reaches off screen at certain position x and destroy…

I’d start with showing it on screen, making it move and then change your position to something like -10 and make it move and then work out your random position. Break it down into steps. For me, reading what you wrote in code, I don’t feel like it will ever make it to the screen or it might once in a while, math depending.

Hi,

Yes, I will break it down it steps, but right now I’m focusing on the random Y Position, I just don’t understand, if I’m getting the position from the node in the editor

Sure, but what if that node is at 100, 100

and you change the Y by -350 or even -101 you are off screen

and it gets even further off if say you are already off screen, (-1, -1). Then you calculate a negative random of -350, now you are further off screen yet.

What is the position of node in the editor when it is on screen?

Hi,

I have the node at position X: 600, and Y: -344 Do you think, it could be an error with the simulator, cause I’m getting these on the console:

Simulator : /Applications/CocosCreator.app/Contents/Resources/app.asar.unpacked/utils/simulator/mac/Simulator.app/Contents/Resources/src/jsb_polyfill.js:16568:Error: js_cocos2dx_Node_setPositionX : Error processing arguments

oh, could be. I hadn’t thought of that. @nantas2 @pandamicro can you take a look at this? error one post above this one?

  1. You are passing wrong arguments to node.setPosition, this.rand == null, your random number is just rand - you have declared it as a local variable, not as a this.rand property;
  2. Math.random() never takes arguments, it’s only usage is to return a random float in range [0,1) so you must convert it to your range manually. Yeah, javascript sucks and it’s not it’s fault :frowning:
  3. I’m not sure about some methods you use here:
var duckmoveleftx = new cc.MoveBy(2, cc._p(-1078,0));

They may work but I would prefer using methods that are declared in official API reference (in Creator’s menu Help->API Reference)

So I’ve tried to fix your code piece for you, hope it works:

onLoad: function () {
	var rand = Math.floor( Math.random() * (360 + 350) - 350 );
	this.node.setPosition(this.duckposx, rand); 
	// Play Duck Fly Anim Clip
	var duckflyleft = this.node.getComponent(cc.Animation);
	duckflyleft.play("duckflyleftanim");
	// Move Left Random 
	var duckmoveleftx = cc.moveBy(2, cc.p(-1078,0));
	// Action Duck 
	this.node.runAction(duckmoveleftx);
},

If it doesn’t please post whatever errors you get.

@slackmoehrle, in Cocos Creator (0,0) is the center of the canvas, and also node coordinates are relative to it’s parent, so negative coordinates are absolutely valid. Don’t confuse people :smile:

You are right, my mistake. I am used to 0,0 not being the center. Thank you for the correction.

Hi Persy,

Thanks for the help, much appreciation, it’s working now…Yeah, javascript is very tedious, it gave me a java-headache yesterday… :grin:

cc._p and cc.p - May I ask, what is the difference?

You know, I come from a background in BASIC, but that was years ago, when the TRS-80 and Commodores C64, Amiga, were around, kind of miss those times… :smile:

I really hope that the development team at Cocos2dx takes into consideration Visual Scripting through nodes, or blocks like scratch, cause it will really will open the doors to non programmers like me or others to use Cocos Creator. Also, please take into consideration adding other languages for programmers, like C++, Lua, Typescript I don’t consider myself a programmer, I’m just a newbie when it comes to Javascript and other languages, but I think that in my opinion, programming a game should be fun, not a headache, and this is where Visual Scripting comes into play. At first I was using Unity and was trying out a visual scripting extension, but I saw the export of huge files, and decided it was not for me…So, I’ve been following Cocos2dx for a while, enjoy the supportive community, and do feel that Cocos Creator is a nice, user friendly editor, which of course will be improved on.

Thanks again for your help, back to programming… :grinning:God Bless…

Sincerely,

Sunday