Golf game trouble

Hello!
I am a beginner in programming and am still trying to research methods of code but have found it very difficult. At the moment I am trying to create a top-down golf game but am having trouble figuring out how to make it. I want it so that when I click and drag the ball, it launches its self. Can anyone help? Thanks!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Hello! I can help with this as my game Microscope Madness uses this same mechanic. :)

So, basically, you want to do the following:

1) Detect the click on the ball by the user
2) Set the range that the user can drag and only permit the user to drag in that range
3) When the user lets go of the mouse button, use the distance from the mouse to the ball to calculate the force and the direction

In code this will look something like the following:

GML:
// CREATE EVENT OF BALL
clicked = false;    // Used to check when the ball is clicked on
moving = false;        // Used to check if the ball is moving
max_dist = 100;        // The maximum distance the mouse can move from the ball
power_scalar = 10;    // Power scalar value used to determine shot power
friction = 0.1;        // General friction

// STEP EVENT
if !moving
{
if !clicked
    {
    // Check if the player clicks the ball
    if mouse_check_button_pressed(mb_left)
        {
        if point_in_circle(x, y, mouse_x, mouse_y, sprite_width / 2)
            {
            clicked = true;
            }
        }
    }
else
    {
    // Check if the player releases the mouse button
    if mouse_check_button_released(mb_left)
        {
        // Get the distance the player moved the mouse and clamp it
        var _dist = point_distance(mouse_x, mouse_y, x, y);
        _dist = clamp(_dist, 0, max_dist);
        // Get the direction the ball has to move in
        var _dir = point_direction(mouse_x, mouse_y, x, y);
        // Move the ball
        hspeed = lengthdir_x(_dist / power_scalar, _dir);
        vspeed = lengthdir_y(_dist / power_scalar, _dir);
        moving = true;
        }
    }
}
That's the basic setup that you'll need right there... Once you get that working as you require to start the movement, you can then refine it to make the game more "golf-like", as this will give you linear movement more like a snooker ball than a golf ball, but that's just a case of tweaking the speeds and adding in some extra checks and variables.
 
Sorry to follow up on a question I thought was complete, but I realized I could only pull back the golf ball once, and then when I try again, it wouldn't work without me reloading the whole game. Why is this?
 
I basically just did the same thing he did. I'm still trying to make games off of what I learn, so it may not be original, but I'm still learning as I said from the start. I'm still learning dude, cut me some slack.

I am genuinely interested in coding and am in the process of getting classes, but for now, I'm teaching myself.

(I'm not gonna post my games btw, just using them for reference)
 
The only reason I initially asked if you understood the code given to you is because it's missing components that keep it from being completely standalone. I was asking for your implementation of it so that we can see where the mistakes lie in order to fix them. You can't just state that it doesn't work and expect us to know the solution when we have no idea what you may or may not have done; it's like telling the mechanic over the phone that you took his advice on fixing the slight oil pan leak but now the car can't start without being jumped.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Yes, the code I posted will only permit the ball to move once as it has nothing in it to detect when the ball can be clicked again... I wasn't wanting to just give a complete piece of code, but rather give you sufficient so that you could learn from it and build upon it! The best way to help people is to try and get them to do it themselves... :)

That said, I can tell you that - unless you have modified the code above a lot - what you need to do is add an "else" into the main Step Event code and check whether the ball has stopped moving or not in that. So the basic structure would be:
GML:
if !moving
{
// Ball isn't moving so check for click and drag that moves the ball
}
else
{
// The ball IS moving, so check to see if it's stopped
}
Once you add this else, you need to check to see if the ball has stopped. This can be done in multiple ways depending on what system of movement you finally go for. Like you can check the hspeed/vspeed values, or you can compare x/y against xprevious/yprevious. Once you have the check, if it returns true then the ball has stopped and you can set the moving and clicked variables back to false to permit further movement.
 
Top