- Home /
Javascript Movement Script. Why am I getting these errors?
Hey,
I am currently working on a input/movement script for a simple top-down 2D RPG using javascript.
Here is what I have written so far:
#pragma strict
/* ========== Define Variables ========== */
var moveSpeed : float = 10.0;
var playerSprite : PackedSprite;
function Update () {
var thisTransform : Transform;
var moving : boolean;
var movingLeft : boolean = false;
var movingRight : boolean = false;
var movingUp : boolean = false;
var movingDown : boolean = false;
var wasMovingLeft : boolean;
var wasMovingRight : boolean;
var wasMovingUp : boolean;
var wasMovingDown : boolean;
var movement : float;
var horMoveDir : int;
var verMoveDir : int;
thisTransform = transform;
/* ========== Keyboard Input ==========*/
if(Input.GetKey("left")) {
horMoveDir = -1;
verMoveDir = 0;
wasMovingLeft = true;
wasMovingRight = false;
wasMovingUp = false;
wasMovingDown = false;
if(!movingLeft) {
movingLeft = true;
movingRight = false;
movingUp = false;
movingDown = false;
AnimateWalk();
}
}
if(Input.GetKey("right")) {
horMoveDir = 1;
verMoveDir = 0;
wasMovingLeft = false;
wasMovingRight = true;
wasMovingUp = false;
wasMovingDown = false;
if(!movingRight) {
movingLeft = false;
movingRight = true;
movingUp = false;
movingDown = false;
AnimateWalk();
}
}
if(Input.GetKey("up")) {
horMoveDir = 0;
verMoveDir = 1;
wasMovingLeft = false;
wasMovingRight = false;
wasMovingUp = true;
wasMovingDown = false;
if(!movingUp) {
movingLeft = false;
movingRight = false;
movingUp = true;
movingDown = false;
AnimateWalk();
}
}
if(Input.GetKey("down")) {
horMoveDir = 0;
verMoveDir = -1;
wasMovingLeft = false;
wasMovingRight = false;
wasMovingUp = false;
wasMovingDown = true;
if(!movingDown) {
movingLeft = false;
movingRight = false;
movingUp = false;
movingDown = true;
AnimateWalk();
}
}
/* ========== Animate Walk ==========*/
function AnimateWalk() {
if(movingLeft) {
playerSprite.PlayAnim(3);
}
if(movingRight) { /*Try "else if" if this doesn't work*/
playerSprite.PlayAnim(2);
}
if(movingUp) {
playerSprite.PlayAnim(0);
}
if(movingDown) {
playerSprite.PlayAnim(1);
}
}
}
The Unity Console is returning the following errors for it:
(111,18): BCE0044: expecting (, found 'AnimateWalk'.
(111,31): UCE0001: ';' expected. Insert semicolon at the end.
(112,17): BCE0043: Unexpected token: if.
(112,31): UCE0001: ';' expected. Insert a semicolon at the end.
(113,49): BCE0044: expecting :, found ';'.
(line 111 is the first after "Animate Walk" comment)
I have no idea why. These characters obviously shouldn't be where it says they should. (should they? :S).
I am very new to Unityscript and I get the feeling I'm missing something ridiculously obvious, but I've spent most of the day trying to figure this out and it's baffling me. Anybody lend a hand?
Answer by Sharon Hanlon · Feb 18, 2012 at 08:47 PM
Your AnimateWalk function is inside of your Update function. Move the } from the last line and put it before the function AnimateWalk() line.
Damn. That simple.
Thanks, I'd probably never have spotted that and still be pulling my hair out now.
Everything is working fine, except my animations. Only the up animation seems to work. =[ (I ended up using else if.) Anyone any idea why? :S
Answer by Sharon Hanlon · Feb 18, 2012 at 10:17 PM
If you're using the tk2DSprite package for your animation, make sure all sprites in the collection have a "bottom left" anchor point. If you're not using that, sorry, I have no idea.
I'm using Sprite $$anonymous$$anager 2, the anchor point is set to the texture offset (0, 0, 0,), changing to bottom left didn't help. Thanks anyway.
I found out my problem is that when I build atlases only the first animation (walkup) is beig drawn, so there is no animation to access on the atlas.