• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Why the screen does not shake? [SOLVED!]

Hi there!,
I've been through Shawn's tutorial, in order to make the screen shake, and I have troubles with this part of the code:
Code:
var random_range = random_range(-shake_value, shake_value);

view_xview[0] += random_range;
view_yview[0] += random_range;
"shake_value" is set to 15 when I press H and then it gets reduced to 0 after. The problem is that this code does nothing; I debugged by printing the xview to screen and nothing changed. You may ask me "have you checked views?" Yes, I do! In fact I use his previous code to move the camera around, but this, just makes no sense... Why doesn't it shake? If you know another way to shake, I would be glad if you let me know!
 
J

JFitch

Guest
You can't use a built in function (random_range) as a variable.

Also, what you're attempting to do would move it an equal amount in the x and y direction, which would look like it's shaking in a straight line and not in random directions. Do this:
Code:
view_xview[0] += random_range(-shake_value, shake_value);
view_yview[0] += random_range(-shake_value, shake_value);
 
You can't use a built in function (random_range) as a variable.

Also, what you're attempting to do would move it an equal amount in the x and y direction, which would look like it's shaking in a straight line and not in random directions. Do this:
Code:
view_xview[0] += random_range(-shake_value, shake_value);
view_yview[0] += random_range(-shake_value, shake_value);
I appreciate that, but still not working :/
 

RangerX

Member
Totally different approach yet super simple:

Imagine you do something like this in the object your view is following.
Put in under a condition in a step event:

if(your condition for shaking)
then
{
Offset=choose(-2,-1,1,2);
x+=Offset;
y+=Offset;

if(x>LastX+2 || x<LastX-2)
then
{
x=LastX;
}

if(y>LastY+2 || y<LastY-2)
then
{
y=LastY;
}
}


Don't forget to catch your initial position before the skake starts (LastX and LastY)
To make a moving and shaking cam is a tad more complex though but you get the idea

 

Bingdom

Googledom
Are you using the built-in camera follow functions or your camera movement code is after the shake? That could be overwriting the camera shake.
 
T

tafkatfos

Guest
did you add this bit after the view_xview[0] += random_range(-shake, shake); etc.

Code:
view_xview[0] += random_range(-shake, shake);
view_yview[0] += random_range(-shake, shake);
// did you add this bit below?
shake *= 0.9;
 

sp202

Member
Even if your code did work, by the time shake_value becomes 0 your view will be offset by a significant amount. Use a variable to toggle shake on and off and record the original view position so you can set it back after the shaking is over:
Code:
if shake=true
{
view_xview[0]=viewx+random_range(-shake,shake)
view_yview[0]=viewy+random_range(-shake,shake)
}
else
{
viewx=view_xview[0]
viewy=view_yview[0]
}
 
Did you try his code example? Because it should work.

If you have and it doesn't then you need to show us more of your code. Could be shake_value that is at fault here.
Here's the full original code that i used: (i tried all your answers, and noone works..., thank you anyway!!!!)
- CREATE -
Code:
/// Initialize the view
mouse_radius = 40;
smooth_animation = 25;

shake_value = 0;
- STEP -
Code:
/// Move the view - (Note: obj_player exists, and the game_status is not FREEZE)
if (!instance_exists(obj_player) or global.game_status == FREEZE) {exit;}

var mouse_dir = point_direction(x, y, mouse_x, mouse_y);
var mouse_dist = point_distance(x, y, mouse_x, mouse_y);

var xtarget = obj_player.x + lengthdir_x( min(mouse_radius, mouse_dist), mouse_dir);
var ytarget = obj_player.y + lengthdir_y( min(mouse_radius, mouse_dist), mouse_dir);

x += (xtarget-x) / smooth_animation;
y += (ytarget-y) / smooth_animation;

view_xview[0] = -(view_wview[0]/2) + x;
view_yview[0] = -(view_hview[0]/2) + y;

view_xview[0] = clamp(view_xview[0], 0, room_width-view_wview[0]);
view_yview[0] = clamp(view_yview[0], 0, room_height-view_hview[0]);

//Shaking - (Note: Not Working Section? (the shake_value goes down correctly!))
view_xview[0] += random_range(-shake_value, shake_value);
view_yview[0] += random_range(-shake_value, shake_value);
shake_value *= 0.9;
- Release H Event -
Code:
/// Shake!
shake_value = 15;
----------------------------------------------------------------
Information about object: obj_view
Sprite: spr_view
Solid: false
Visible: false
Depth: 0
Persistent: true
Parent:
Children:
Mask:
No Physics Object
----------------------------------------------------------------

I have to say that it does not work in any room, not even at the center! And the movement works perfectly!!!
 
Top