[SOLVED] Getting toString error on keyboard isPressed

i have this problem

var Movement = pc.createScript('Movement');

Movement.attributes.add('camera', {
    type: 'entity',
    description: 'Optional, assign a camera entity, otherwise one is created'
});

Movement.attributes.add('power', {
    type: 'number',
    default: 2500,
    description: 'Adjusts the speed of player movement'
});

Movement.attributes.add('lookSpeed', {
    type: 'number',
    default: 0.25,
    description: 'Adjusts the sensitivity of looking'
});

// initialize code called once per entity
Movement.prototype.initialize = function() {
    this.force = new pc.Vec3();
    this.eulers = new pc.Vec3();

    var app = this.app;

    // Listen for mouse move events
    app.mouse.on("mousemove", this._onMouseMove, this);

    // when the mouse is clicked hide the cursor
    app.mouse.on("mousedown", function () {
        app.mouse.enablePointerLock();
    }, this);

    // Check for required components
    if (!this.entity.collision) {
        console.error("First Person Movement script needs to have a 'collision' component");
    }

    if (!this.entity.rigidbody || this.entity.rigidbody.type !== pc.BODYTYPE_DYNAMIC) {
        console.error("First Person Movement script needs to have a DYNAMIC 'rigidbody' component");
    }
    
    this.rounds = 12;

    if (this.app.mouse) {
        this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
        this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this);
        this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
    }
};

// update code called every frame
Movement.prototype.update = function(dt) {
    // If a camera isn't assigned from the Editor, create one
    if (!this.camera) {
        this._createCamera();
    }

    var force = this.force;
    var app = this.app;

    // Get camera directions to determine movement directions
    var forward = this.camera.forward;
    var right = this.camera.right;


    // movement
    var x = 0;
    var z = 0;

    // Use W-A-S-D keys to move player
    // Check for key presses
    if (app.keyboard.isPressed(pc.KEY_A) || app.keyboard.isPressed(pc.ARROW_UP)) {
        x -= right.x;
        z -= right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_D)) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W)) {
        x += forward.x;
        z += forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_S)) {
        x -= forward.x;
        z -= forward.z;
    }

    // use direction from keypresses to apply a force to the character
    if (x !== 0 && z !== 0) {
        force.set(x, 0, z).normalize().scale(this.power);
        this.entity.rigidbody.applyForce(force);
    }

    // update camera angle from mouse events
    this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);
    
    // reload gun magazine
    if (app.keyboard.isPressed(pc.KEY_R)) {
        this.rounds = 12;
    }
};

Movement.prototype._onMouseMove = function (e) {
    // If pointer is disabled
    // If the left mouse button is down update the camera from mouse movement
    if (pc.Mouse.isPointerLocked() || e.buttons[0]) {
        this.eulers.x -= this.lookSpeed * e.dx;
        this.eulers.y -= this.lookSpeed * e.dy;
    }
};

Movement.prototype._createCamera = function () {
    // If user hasn't assigned a camera, create a new one
    this.camera = new pc.Entity();
    this.camera.setName("First Person Camera");
    this.camera.addComponent("camera");
    this.entity.addChild(this.camera);
    this.camera.translateLocal(0, 0.5, 0);
};

Movement.prototype.onMouseDown = function(event) {
    if (event.button === pc.MOUSEBUTTON_LEFT) {
        // shoot
        if (this.rounds !== 0) {
            var bullet = this.app.root.findByName('Bullet').clone();
            this.app.root.addChild(bullet);
            bullet.reparent(this.app.root);
            bullet.setPosition(this.app.root.findByName('Bullet').getPosition());
            bullet.setRotation(this.app.root.findByName('Bullet').getRotation());
            bullet.enabled = true;
            console.log(bullet);
            // update magazine
            this.rounds = this.rounds - 1;
        }
    }
};

Where did you get pc.ARROW_UP from?

1 Like

Hi @thebosser24,

Change the code above to:

app.keyboard.isPressed(pc.KEY_UP)

Thank you
MAJOR UPDATE

1 Like

i was trying it myself

this is similar to the ToString thing, but i was working with a dodge mechanism

if (this.app.keyboard.wasPressed(pc.KEY_Q) === true) {
    this.entity.rigidbody.applyImpulse(0, 0.1, 1);  
}
if (this.app.keyboard.wasPressed(pc.KEY_E) === true) {
    this.entity.rigidbody.applyImpulse(0, 0.1, -1);  
}

and it says

Couple of things:

  1. this.app is undefined for some reason. The reason will depend on the surrounding code.
  2. You don’t need to compare against true. You can just do:
if (this.app.keyboard.wasPressed(pc.KEY_Q)) {

this is all of it }
}
};
if(this.app.keyboard.wasPressed(pc.KEY_Q));
this.entity.rigidbody.applyImpulse(0, 0.1, 1);
}
if(this.app.keyboard.wasPressed(pc.KEY_E)
this.entity.rigidbody.applyImpulse(0, 0.1, -1);
}

it say this image

You should not have a semicolon here:

if(this.app.keyboard.wasPressed(pc.KEY_Q));

Might I suggest this course to get more familiar with Javascript syntax?

but, it says Missing semicolon, unrecoverable syntax error(97% scanned)image

You removed the incorrect semicolon.

Literally on this line above that (145):

if(this.app.keyboard.wasPressed(pc.KEY_Q));

You should not have a semicolon as you are declaring your if statement.

1 Like

i did that, but here it is now

image

What about line 153? Is that semicolon also there by mistake?

Hi @eproasim,

I think it’s supposed to be there? };

no,it shows a red X
its supposed to be there

Without seeing your entire script, I am unable to provide you with a proper answer. Based on the the small amount of structure I can see, my guess is that a semicolon should not be there.

We rely on complete information to provide others with help. Please consider providing complete information in the future.

Is your code inside your update function @thebosser24?

no.