Can I use on activate to trigger a repeated action?

28 views
Skip to first unread message

Christopher Cook

unread,
Dec 9, 2016, 2:30:55 PM12/9/16
to CoSpaces
Say I want an object, when touched, to start moving about randomly but returning to a "home" position after each movement.

Susanne Krause

unread,
Dec 14, 2016, 4:20:39 AM12/14/16
to CoSpaces
Hello Christopher, we've created some code as an example for you. Here, the bunny starts moving randomly when you click it and returns to its starting point after some time.
You should be able to adapt the code for your purposes:
 

var rabbit = Space.createItem('LP_Rabbit', 0, 0, 0);
var base = { x: 0, y: 0, z: 0 };
var turns = 5;

function randNumBetween(min, max) {
    return Math.random() * (max - min) + min;
}

function moveRandomly() {
    rabbit.say('moving randomly');
    if(turns > 0) {
        rabbit.move(randNumBetween(-3, 3), randNumBetween(-3, 3), 0, moveRandomly);
        turns--;
    } else {
        rabbit.say('returning to base');
        rabbit.moveTo(base.x, base.y, base.z);
        turns = 5;
    }
}

rabbit.onActivate(moveRandomly);


I hope we could help you with this!
Reply all
Reply to author
Forward
0 new messages