CUSTOM FUNCTION: Requires no editing or customizationMouse Look: Look around the game world using just your mouse (as is done in first person shooter games). This works for 2D as well as simulated 3D GE games.Features:1) Look around the screen (auto scroll) horizontally and vertically using just the mouse cursor
2) Locks the crosshairs at the center of the screen (the crosshairs are an actor - the mouse cursor is to be hidden as explained below)
3) The scroll speed is controlled by the speed with which the mouse is moved
4) Allows the user to continue scrolling even after the mouse cursor hits the edge of the screen. However, at this point, the scroll speed will be equal to the mouse scroll speed immediately before the cursor hit the edge of the screen.
5) Prevents the view actor from moving outside the background image area while scrolling in 2D games. This is managed dynamically by the program and automatically adjusts according to the image and view size
6) In simulated 3D games, when the view reaches the edge of the screen, it continues moving from the opposite side of the screen to provide an illusion of a seamless, 3D environment. This requires the background image to have contain the exact same view at the far left and right of the image. The same holds true for the top and bottom. This is done dynamically as well, although it is currently limited to a resolution of 1360 X 768
7) Although not included in the game code posted in this thread, the attached demo contains a feature that actively displays the X and Y mouse scroll speed in real time.
Requirements:To enable mouse look, you will need:
1) An actor for the crosshairs
2) An actor for the background
3) To copy the function code into the Global Code and save it
4) To hide the mouse cursor (recommended), go to Config -> Game Properties, then click the drop down menu in the screenshot below and select "Hide Mouse"
5) If this is going to be used for a simulated 3D game, the background image will need to be set up so that the edges of the screen corresponding to the view size are identical. Additional efforts may be needed to make actors at the edge of the screen seem consistent (this does not include the Background, view and Crosshair actors).
Using this Function:To use mouse look, simply paste the following function in your "view" actor's "Draw" event and replace the function parameters to match your game actors. These are explained immediately below the code.
- Code: Select all
mouseLook("Background", "Crosshairs", "3D");
"Background" - This is the name of the actor that displays the background image of your game
"Crosshairs" - This is the name of the actor that displays the crosshairs at the center of the screen
"3D" - This denotes whether the game is a 2D game or a simulated 3D game. Enter "2D" for a 2D game and "3D" for a 3D game. The quotes are necessary.
Function Code: Copy the following code into the Global Code and save it there.
- Code: Select all
int xmouse_prev=xmouse;
int ymouse_prev=ymouse;
int xscroll_speed=0;
int yscroll_speed=0;
void mouseLook(char actor_background[30], char actor_crosshairs[30], char game_dimensions[4]) {
if(xmouse!=xmouse_prev) { // If the horizontal mouse position has changed
view.x+=(xmouse-xmouse_prev); // Moving the view along with the mouse
getclone(actor_crosshairs)->x+=(xmouse-xmouse_prev); // Moving the crosshairs along with the view
xscroll_speed=abs(xmouse_prev-xmouse);
xmouse_prev=xmouse; // Setting value of xmouse_prev to xmouse for the next frame
}
else if (xmouse<=20) { // If the mouse is at the extreme left of the screen
view.x-=xscroll_speed; // Moving the view to the left
getclone(actor_crosshairs)->x-=xscroll_speed; // Moving the crosshairs to the left
}
else if (xmouse>=view.width-40) { // If the mouse is at the extreme right of the screen
view.x+=xscroll_speed; // Moving the view to the left
getclone(actor_crosshairs)->x+=xscroll_speed; // Moving the crosshairs to the right
}
if (strcmp(game_dimensions,"2D")==0&&view.x<=getclone(actor_background)->x-(getclone(actor_background)->width/2)) // If the game mode is 2D and view is at the left of the background image
view.x=getclone(actor_background)->x-(getclone(actor_background)->width/2); // Stop moving left
else if (strcmp(game_dimensions,"3D")==0&&view.x<=getclone(actor_background)->x-(getclone(actor_background)->width/2)+(1360-view.width)+xscroll_speed) // If the game mode is 3D and view is at the left of the background image
// Jump to the right of the screen minus the xscroll_speed (that is the distance the screen would move in 1 frame)
view.x=getclone(actor_background)->x+(getclone(actor_background)->width/2)-view.width-xscroll_speed-(1360-view.width)+10;
else if (strcmp(game_dimensions,"2D")==0&&view.x+view.width>=getclone(actor_background)->x+(getclone(actor_background)->width/2)) // If the view is at the right of the background image
view.x=getclone(actor_background)->x+(getclone(actor_background)->width/2)-view.width; // Stop moving right
else if (strcmp(game_dimensions,"3D")==0&&view.x+view.width>=getclone(actor_background)->x+(getclone(actor_background)->width/2)-(1360-view.width)-xscroll_speed) // If the game mode is 3D and view is at the right of the background image
// Jump to the left of the screen plus the xscroll_speed (that is the distance the screen would move in 1 frame)
view.x=getclone(actor_background)->x-(getclone(actor_background)->width/2)+xscroll_speed+(1360-view.width)-10;
getclone(actor_crosshairs)->x=view.x+(view.width/2);
if (strcmp(game_dimensions,"2D")==0&&view.x<=getclone(actor_background)->x-(getclone(actor_background)->width/2)) // If the game mode is 2D and view is at the left of the background image
view.x=getclone(actor_background)->x-(getclone(actor_background)->width/2); // Stop moving right
else if (strcmp(game_dimensions,"3D")==0&&view.x<=getclone(actor_background)->x-(getclone(actor_background)->width/2)+(768-view.width)) // If the game mode is 3D and view is at the left edge of the background image
// Jump to the right of the screen minus the xscroll_speed (that is the distance the screen would move in 1 frame)
view.x=getclone(actor_background)->x+(getclone(actor_background)->width/2)-view.width-xscroll_speed-(768-view.width)-10;
else if (strcmp(game_dimensions,"2D")==0&&view.x+view.width>=getclone(actor_background)->x+(getclone(actor_background)->width/2)) // If the view is at the right edge of the background image
view.x=getclone(actor_background)->x+(getclone(actor_background)->width/2)-view.width; // Stop moving left
else if (strcmp(game_dimensions,"3D")==0&&view.x+view.width>=getclone(actor_background)->x+(getclone(actor_background)->width/2)-(768-view.width)) // If the game mode is 3D and the view is at the right edge of the background image
// Jump to the left of the screen plus the xscroll_speed (that is the distance the screen would move in 1 frame)
view.x=getclone(actor_background)->x-(getclone(actor_background)->width/2)+xscroll_speed+(768-view.width)+10;
getclone(actor_crosshairs)->x=view.x+(view.width/2);
if(ymouse!=ymouse_prev) { // If the vertical mouse position has changed
view.y+=(ymouse-ymouse_prev); // Moving the view along with the mouse
getclone(actor_crosshairs)->y+=(ymouse-ymouse_prev); // Moving the crosshairs along with the view
yscroll_speed=abs(ymouse_prev-ymouse);
ymouse_prev=ymouse; // Setting value of ymouse_prev to ymouse for the next frame
}
else if (ymouse<=20) { // If the mouse is at the top edge of the screen
view.y-=yscroll_speed; // Moving the view up
getclone(actor_crosshairs)->y-=yscroll_speed; // Moving the crosshairs up
}
else if (ymouse>=view.height-40) { // If the mouse is at the bottom edge of the screen
view.y+=yscroll_speed; // Moving the view down
getclone(actor_crosshairs)->y+=yscroll_speed; // Moving the crosshairs down
}
if (strcmp(game_dimensions,"2D")==0&&view.y<=getclone(actor_background)->y-(getclone(actor_background)->height/2)) // If the game mode is 2D and view is at the top of the background image
view.y=getclone(actor_background)->y-(getclone(actor_background)->height/2); // Stop moving up
else if (strcmp(game_dimensions,"3D")==0&&view.y<=getclone(actor_background)->y-(getclone(actor_background)->height/2)+(768-view.height)) // If the game mode is 3D and view is at the top of the background image
// Jump to the bottom of the screen minus the yscroll_speed (that is the distance the screen would move in 1 frame)
view.y=getclone(actor_background)->y+(getclone(actor_background)->height/2)-view.height-yscroll_speed-(768-view.height)-10;
else if (strcmp(game_dimensions,"2D")==0&&view.y+view.height>=getclone(actor_background)->y+(getclone(actor_background)->height/2)) // If the view is at the bottom of the background image
view.y=getclone(actor_background)->y+(getclone(actor_background)->height/2)-view.height; // Stop moving down
else if (strcmp(game_dimensions,"3D")==0&&view.y+view.height>=getclone(actor_background)->y+(getclone(actor_background)->height/2)-(768-view.height)) // If the game mode is 3D and the view is at the bottom of the background image
// Jump to the top of the screen plus the yscroll_speed (that is the distance the screen would move in 1 frame)
view.y=getclone(actor_background)->y-(getclone(actor_background)->height/2)+yscroll_speed+(768-view.height)+10;
getclone(actor_crosshairs)->y=view.y+(view.height/2);
}
Additional Options:To begin the game with the screen centered in the original view position, paste the following code into the "view" actor's "Create" event. This is to avoid scrolling or an unexpected screen starting position when your game launches.
- Code: Select all
// Initializing mouse position in variables
xmouse_prev=xmouse;
ymouse_prev=ymouse;
Example GED File: Attached - A sample simulated 3D game environment. The world map continues scrolling as though the player is moving "around" the screen.