// Create a PlayCanvas application
var canvas = document.getElementById("render");
var app = new pc.Application(canvas, {
keyboard: new pc.Keyboard(window)
});
app.start();
// Fill the available space at full resolution
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
// Create box entity
var cube = new pc.Entity();
cube.addComponent("model", {
type: "box"
});
// Create camera entity
var camera = new pc.Entity();
camera.addComponent("camera", {
clearColor: new pc.Color(0.1, 0.1, 0.1)
});
// Create directional light entity
var light = new pc.Entity();
light.addComponent("light");
// Add to hierarchy
app.root.addChild(cube);
app.root.addChild(camera);
app.root.addChild(light);
// Set up initial positions and orientations
camera.setPosition(0, 0, 3);
light.setEulerAngles(45, 0, 0);
app.on('update', function(dt) {
if (app.keyboard.wasPressed(pc.KEY_LEFT)) {
cube.rotate(0, -20*dt, 0);
}
});
I am not really following the above example. I am having the same problem - keyboard.wasPressed is running the same way as keyboard.isPressed. Could you please tell me how to fix my code?
Rgds,
var CodeJjkk = pc.createScript('codeJjkk');
// initialize code called once per entity
CodeJjkk.prototype.initialize = function() {
};
// update code called every frame
CodeJjkk.prototype.update = function(dt) {
//declaration
var codpos = 1;
var count = 1;
var VarOfGreatness = this.app.keyboard.wasPressed(pc.KEY_1);
//logic
for (count = 0; count < 10; count ++){
if (VarOfGreatness){
if (codpos === 1 && this.app.keyboard.wasReleased === 0){
fd = this.app.root.findByName('Forward1');
fd.setLocalPosition(-1.106, 1.842, -5.225);
codpos ++;
}
else if (codpos === 2 && this.app.keyboard.wasReleased === 0){
fd = this.app.root.findByName('Forward2');
fd.setLocalPosition(-0.57, 1.84, -5.225);
codpos ++;
}
else if (codpos === 3 && this.app.keyboard.wasReleased === 0){
fd = this.app.root.findByName('Forward3');
fd.setLocalPosition(-0.071, 1.84, -5.225);
codpos ++;
}
else if (codpos === 4 && this.app.keyboard.wasReleased === 0){
fd = this.app.root.findByName('Forward4');
fd.setLocalPosition(0.429, 1.84, -5.225);
codpos ++;
}
else if (codpos === 5 && this.app.keyboard.wasReleased === 0){
fd = this.app.root.findByName('Forward5');
fd.setLocalPosition(0.95, 1.84, -5.225);
codpos ++;
}
else if (codpos === 6 && this.app.keyboard.wasReleased === 0){
fd = this.app.root.findByName('Forward6');
fd.setLocalPosition(-1.1, 1.392, -5.225);
codpos ++;
}
else if (codpos === 7 && this.app.keyboard.wasReleased === 0){
fd = this.app.root.findByName('Forward7');
fd.setLocalPosition(-0.608, -1.392, -5.225);
codpos ++;
}
else if (codpos === 8 && this.app.keyboard.wasReleased === 0){
fd = this.app.root.findByName('Forward8');
fd.setLocalPosition(0.02, 1.392, -5.225);
codpos ++;
}
else if (codpos === 9 && this.app.keyboard.wasReleased === 0){
fd = this.app.root.findByName('Forward9');
fd.setLocalPosition(0.525, 1.392, -5.225);
codpos ++;
}
else if (codpos === 10 && this.app.keyboard.wasReleased === 0){
fd = this.app.root.findByName('Forward10');
fd.setLocalPosition(1.007, 1.392, -5.225);
codpos ++;
}
}
}
};
// swap method called for script hot-reloading
// inherit your script state here
// CodeJjkk.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/