Oh, nice.
Edit: Sorry for all the questions, but I've tried to figure this out on my own and I tried googling and I'm stuck. In Sphere, where do I put a game loop if I'm also running the Map Engine, and how do I structure that loop? Like, let's say I want a counter continuously looping.
var gameCounter = 0;
function loopForever() {
if (gameCounter > 999) {
gameCounter = 0;
}
else {
gameCounter += 1;
}
}
function game() {
CreatePerson("player1", "spWar.rss", false);
AttachInput("player1");
MapEngine("test_map.rmp", 60);
}
I know it's crappy code, I'm just testing out here. The problem is...
1) If I call loopForever at the end of loopForever, and right above MapEngine, the game crashes from too much recursion as soon as I load it.
2) If I do the same but call loopForever after MapEngine, the game runs fine until I close the game, at which point I get the too much recursion error.
3) If I create a custom loop that calls MapEngine as well as loopForever, and the custom loop calls itself, same prob -- recursion errors.
I guess I just don't know how to do loops in Sphere, 'cause I've done them in Canvas just fine. It's a lot of code, but here's my code from the beginning of a Wii U Canvas game I was working on. Right now, all you can do is move a colored circle around the Canvas and change its size and color.
<!DOCTYPE html><html><body>
<script type="text/javascript">
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 854;
canvas.height = 480;
document.body.appendChild(canvas);
// Creates the canvas and sticks it on the page.
var i = -1;
// Necessary starting value for the fiveLoopChecker function.
var fiveLoopRunning = 0;
// So you can check when it starts and stops.
var circleX = canvas.width / 2;
var circleY = canvas.height / 2;
var circleRadius = 24;
// Starts the circle in the center of the canvas with a radius of 24.
var dRed = 255;
var dGreen = 0;
var dBlue = 0;
var sRed = (dRed).toString(16);
var sGreen = (dGreen).toString(16);
var sBlue = (dBlue).toString(16);
var circleColor = "#" + sRed + sGreen + sBlue;
// Variables for the changeColor function. Starts the circle as red.
function initializeGamePad(){
wiiu.gamepad.update();}
// Required to access GamePad controls.
function moveCircle(){
circleX += (wiiu.gamepad.lStickX * 4) + (wiiu.gamepad.rStickX * 4);
circleY -= (wiiu.gamepad.lStickY * 4) + (wiiu.gamepad.rStickY * 4);}
// Moves the circle if the Wii U Gamepad analog sticks are being moved. Use both for SuperSpeed :p
function noClip(){
if (circleX - circleRadius < 0){circleX = circleRadius;}
if (circleX + circleRadius > canvas.width){circleX = canvas.width - circleRadius;}
if (circleY - circleRadius < 0){circleY = circleRadius;}
if (circleY + circleRadius > canvas.height){circleY = canvas.height - circleRadius;}}
// Stops the circle from going off the edge of the canvas.
function clearScreen(){
ctx.clearRect(0, 0, canvas.width, canvas.height);}
// Erases the canvas. Kind of...kind of an obvious one, there >_>
function drawCircle(){
ctx.beginPath();
ctx.arc(circleX, circleY, circleRadius, 0, Math.PI*2, true);
ctx.fillStyle=circleColor;
ctx.fill();}
// Draws a filled circle with radius circleRadius at (circleX, circleY).
function changeCircleSize(){
if ((wiiu.gamepad.hold & 2048) && circleRadius > 1)
{circleRadius -= 1};
if (wiiu.gamepad.hold & 1024)
{circleRadius += 1};}
// Changes the size of the circle when D-Pad left (2048) or right (1024) is pressed.
function changeCircleColor(){
if ((wiiu.gamepad.hold & 512) && dRed < 255){dRed++}
if ((wiiu.gamepad.hold & 256) && dRed > 0){dRed--}
if ((wiiu.gamepad.hold & 8) && dGreen < 255){dGreen++}
if ((wiiu.gamepad.hold & 4) && dGreen > 0){dGreen--}
if ((wiiu.gamepad.hold & 33554432) && dBlue < 255){dBlue++}
if ((wiiu.gamepad.hold & 67108864) && dBlue > 0){dBlue--}}
// Changes the RGB value of the circle.
// Red: D-Pad Up/Down
// Green: Plus/Minus buttons
// Blue: Right Stick Right/Left
function fiveLoopChecker(){
switch (true){
case (i < 0):
fiveLoopRunning = 0;
break;
case (i > -1 && i < loopEndValue):
loopThisFunction();
i ++;
break;
case (i == loopEndValue):
i == -1;
break;}}
// Emulates a for loop, but doesn't lock up the system since one iteration is run per frame.
function fiveLoop(loopMe,thisManyTimes){
loopThisFunction = loopMe;
loopEndValue = thisManyTimes;
i = 0;
fiveLoopRunning = 1;}
// Plug in a function (which is stored in a variable and without parenthesis) and a value (in frames) to loop the function.
function jamesLoopton(){
initializeGamePad();
moveCircle();
noClip();
changeCircleSize();
clearScreen();
drawCircle();
fiveLoopChecker();}
// The game loop.
setInterval(jamesLoopton, 16);
// Runs the game loop every sixteen milliseconds. Almost synchs up with 60 FPS.
</script></body></html>
I'm not as dumb as I let on -- I churned out that Wii U thing all on my own, with nothing but the Mozilla Javascript reference and a few questions asked to a coding forum. I just have no idea how to make stuff run continuously in Sphere.