- Home /
Gameboard, next position on a path
I'm creating a game with a sort of 'Candy Land' mechanic, but with dice... Roll a dice, move piece as many spaces as the dice rolled. I need to get the player's current position and define its next position (pseudo :: nextpos = currentPos + 1; moveTo nextPos x diceRoll). I thought the best way would be defining the gameboard in an array, and nextPos would basically be the next key in the array, but I'm still unsure how to do that, or even if it's the best solution. Any guidance is appreciated.
Edit :: I'm using JavaScript in unity. The path traveled is linear, so where I'm really needing help is how to 'go to' the next position in an array from the current position. I'm not sure how to code 'this key in the array is the current position. this key + 1 is the next position, and the next position x diceroll is where the piece should move to'.
Answer by Jason B · Dec 27, 2010 at 11:29 PM
The path traveled is linear, so where I'm really needing help is how to 'go to' the next position in an array from the current position.
It's difficult to explain, but it should be very straightforward. In theory, each array variable holds a position on the game board, correct?
And each array space is numbered.
I'm pretty sure that all you have to do is, at the start, assign your game piece's position to, for instance, BoardSquare[0], where array slot 0 is the game board starting place (I'm also explaining in C# terms as I don't know Javascript, sorry about that). Also, keep a variable inside the game piece that "remembers" which board square array slot it's occupying. Let's say it's called myCurrentSquare.
From there, it's extremely simple. If you roll a 6, you'd tell the piece to move itself to BoardSquare[(myCurrentSquare + 6)]. It should then move to the 6th array vector.
Does that make sense?
Actually, that'd be the 7th array vector if you start counting from zero, but you know what I mean. :)
yesss, this totally makes sense. i didn't know you could do an equation like that (myCurrentSquare + 6) in the array. will give this a go tonight.
Yep. The array number is just an integer. You can plug in any integer variable name into it ins$$anonymous$$d, or even math (I'm not 100% if you can do math inside the array brackets, but I'm fairly sure you can).
Answer by Jesse Anders · Dec 27, 2010 at 10:33 PM
If the path is strictly linear, then an array would likely be a reasonable solution. If you need help with the details, let us know what language you're using, and what part of the implementation you need help with. (You can edit your original post and add this information.)
Your answer
Follow this Question
Related Questions
How to add LineRenderer positions based on an Array of GameObjects 2 Answers
How to Slerp through values inside a 2D array ? 1 Answer
Problem with the climbing of stairs ( character piloted by coordinates) 0 Answers
Enemy offset, gallaga style game (space invaders) 0 Answers
Instantiating for every Xpos and Zpos 0 Answers