Discord
Login
Community
DARK THEME

Cursor postion not being detected.

Can microstudio locate your mouse once you move out of the initial game window? For context, I added this line of code "screen.setTranslation(-x, -y)" you can see the code below. This made the game centre around the player. However, I also have a piece of code that lets my sword point towards the users cursor, but it stops working once I leave some kind of invisible box. Please help.

  constructor = function()
    this.x = 0
    this.y = 0
    this.speed = 5
    this.sword = "player_with_sword"
    this.sword_length = 25
    this.angle = 0       // angle for the sword
    this.attack_timer = 0
    this.trigger = object end
    this.trigger.x = 0
    this.trigger.y = 0
  end



  update = function()
    // direction check, short form without loops (just maths...)
    this.y += (keyboard.W-keyboard.S)*this.speed
    this.x += (keyboard.D-keyboard.A)*this.speed
    // calculating the angle from the player to the mouse position
    // +30 is the offset for the sword position 
    this.angle = getAngle(this.x+30,this.y,mouse.x,mouse.y)-45 // the sword is drawn under 45 degree
    if mouse.left then
      this.attack_timer = 15
    end
    
    if this.attack_timer > 0 then
      this.attack_timer -= 1
    end
    calculate_position_trigger()
  end
  
  calculate_position_trigger = function()
    this.trigger.x = this.x + 30 + this.sword_length * cosd( this.angle + this.attack_timer * 6 + 45 )
    this.trigger.y = this.y + this.sword_length * sind( this.angle + this.attack_timer * 6 + 45)
  end

  draw = function()
    screen.clear()
    screen.setTranslation(-x, -y)
    screen.drawMap( "map", 0, 0, 300, 200 )
    screen.drawSprite("player", this.x - camera.x, this.y - camera.y, 40, 40 )
    screen.setDrawRotation(this.angle + this.attack_timer*6) // change the drawing angle for the sword
    screen.drawSprite("player_with_sword", this.x+30 - camera.x, this.y - camera.y, 40, 40)
    screen.setDrawRotation(0)  // reset
    screen.drawRound( this.trigger.x, this.trigger.y,  5, 5 )

//    print( this.attack_timer)
  end
  
end
// helper function
// finds the angle in degrees from point 1 to point 2
getAngle = function(x1, y1, x2, y2)
  return atan2d(y2 - y1, x2 - x1)
end

https://microstudio.io/i/Loginus/testrun22/ - improved locating the mouse and drawing the sword towards it.

thanks

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community