PDA

View Full Version : Move() set x and y


blackbeltben
06-16-2014, 11:23 PM
I have been messing around with alternate ways to move my "bullet."

I have been using the move() function a little bit but I cannot get it accurate.

function onCreated(){
setImg("blackbeltben_bullet.png");
setShape(1, 32, 32);
onTimeOut();
scheduleEvent(5, "onDelete");
//the new x and y were set in the putNPC
this.newx = this.newx
this.newy = this.newy
//in this case, the newx and newy were set as the curret mousex and mousey at the point of the creation
}

function onTimeOut(){
setTimer(0.1);
//Here I'd like it to move to this.newx, this.newy
}

function onDelete(){
destroy();
}


If I am using a move(), I should not be putting it in a timeOut. But I'm assuming that there is no way to set a specific x and y with move().
With that being said, I'm guessing I have to calculate how much the x and y need to be increased/decreased... Which I am not sure how to do..


straight to the point
I want my NPC to move directly to this.newx, this.newy at a speed of 0.5;

fowlplay4
06-16-2014, 11:56 PM
function onCreated() {
setImg("blackbeltben_bullet.png");
setShape(1, 32, 32);
moveBullet();
}

function movebullet() {
temp.dx = this.newx - this.x; // delta = final position - initial position
temp.dy = this.newy - this.y;
temp.dist = vectordist({this.x, this.y, 0}, {this.newx, this.newy, 0});
temp.speed = 0.5;
temp.time = temp.dist / temp.speed;
move(temp.dx, temp.dy, temp.time, 0);
setTimer(temp.time);
}

function onTimeout() {
destroy();
}

blackbeltben
06-17-2014, 12:23 AM
function onCreated() {
setImg("blackbeltben_bullet.png");
setShape(1, 32, 32);
moveBullet();
}

function movebullet() {
temp.dx = this.newx - this.x; // delta = final position - initial position
temp.dy = this.newy - this.y;
temp.dist = vectordist({this.x, this.y, 0}, {this.newx, this.newy, 0});
temp.speed = 0.5;
temp.time = temp.dist / temp.speed;
move(temp.dx, temp.dy, temp.time, 0);
setTimer(temp.time);
}

function onTimeout() {
destroy();
}



Please forgive me if this sounds like a dumb question.. But what is the point of putting temp? Couldn't you just put this.dy, this.dist... etc

Keep in mind I don't know much about "temp" stuff

fowlplay4
06-17-2014, 02:00 AM
Please forgive me if this sounds like a dumb question.. But what is the point of putting temp? Couldn't you just put this.dy, this.dist... etc

Keep in mind I don't know much about "temp" stuff

Well it's for temporary values/variables. When you use this. or no prefix at all those variables stay in memory with the npc / globally.

Using temp also guarantees the variable will be empty/0 and won't conflict with other parts of your script.

Quick example:



// output:
// 0
// 245
// 123

function onCreated() {
temp.a = 123;
test();
echo(temp.a);
}

function test() {
echo(temp.a + 0);
temp.a = 245;
echo(temp.a);
}

Torankusu
06-17-2014, 08:06 AM
^^ what he said, simplified, temp variables (values) exist primarily within the function they are specified (unless otherwise passed and named).

"this" variables are accessible throughout the script.
"temp" variables are accessible through a function but discarded and not maintained in memory.
they can still be passed to another function.