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…
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…
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.
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…
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?
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;
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
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
Thanks for the help, much appreciation, it’s working now…Yeah, javascript is very tedious, it gave me a java-headache yesterday…
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…
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… God Bless…