jeudi 2 avril 2015

Attempting to correct the position from which the "player" follows the control(cursor)

I have started a new project and decided to do the hardest part of the program first, when I say hardest I mean the niggly bits.


The code below has my player following the mouse cursor but the movement is constant and the player's position is relative to the cursor's position to the screen and not the actual program window.The closer the window it to the top corner of the screen the closer the cursor and player become.


I am looking for assistance to correct this so that the player's position moves to the cursor's at a certain speed but actually follows the pointer.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace games1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Click(object sender, MouseEventArgs e)
{
Invalidate();
}
private void tmrMoving_Tick_1(object sender, EventArgs e)
{
if (tmrMoving.Enabled == true)
{
var cursPoint = new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y);
var playerPoint = new System.Drawing.Point(player.Location.X, player.Location.Y);
var diff = new Point(Cursor.Position.X - playerPoint.X, Cursor.Position.Y - playerPoint.Y);
var speed = Math.Sqrt(diff.X * diff.X + diff.Y * diff.Y);
if (speed > 10)
{
diff.X /= (int)(speed / 10);
diff.Y /= (int)(speed / 10);
}
player.Location = new System.Drawing.Point(player.Location.X + diff.X, player.Location.Y + diff.Y);
}
}
}
}

Aucun commentaire:

Enregistrer un commentaire