Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualG4MR

Posted 07 January 2013 - 04:25 AM

So I attempted to do what you said and I had to make the speed the same for the player and the zombie.

 

Problem/Question 1: It seems I can't make both units on the screen different velocities (eg.: player1 = zombie with a speed of 2 and player2 = player with a speed of 3 [slightly faster]), if I do this and I go in a diagonal direction my player1 sprite will go in weird circular patterns unless I stop moving.

 

Problem/Question 2: It seems that if I allow diagonal movement (holding W and D or A & S, etc...) it will do this small curl up until I stop moving.  What's the best way to approach this problem?  

 

Problem/Question 3: There's also the issue when holding just down or right (S or D) and the sprite will just sit on the X or Y axis until I stop moving the unit on the screen and then proceed to move towards my unit.  How do I sort this?

Example:

Try what I'm talking about: http://morezombies.net/Zoms.zip (Virus Total Link: https://www.virustotal.com/url/1bb28babfa7e3049adc0f11e3f85c4a063728b799c20d4968e1c6c25921fc474/analysis/1357552685/)

Source:

 

#include "GameApp.h"

void GameApp::Setup() {

	//setup the game window
	window.create(VideoMode(width, height), "Zoms");

	//set framerate
	window.setFramerateLimit(60);

	player1.setSize(10, 20);
	player1.setPos(10, 10);

	player2.setSize(10,10);

	//lets say theg ames running now
	running 	= true;
}

void GameApp::Run() {

	//Setup game
	Setup();
	
	//used to get the current angle of the line between the two points
	//then we can properly move the sprite to the player
	double angle, x, y, newx, newy;

	//sprite speed 
	int speed 	= 2;

	//are the wasd buttons pressed?
	bool mw = false;
	bool ma = false;
	bool ms = false;
	bool md = false;

	//setup the player2 location
	int ranx 	= 300;
	int rany 	= 300;

	player2.setPos(ranx, rany);

	float timeprev, timecurr;

	while(window.isOpen()) {
		
		//loop through the active events (keyboard, mouse, etc...)
		while(window.pollEvent(event)) {

			if(event.type == Event::Closed) {
				window.close();
			}

			if(event.type == Event::KeyPressed) {
				switch(event.key.code) {
					case Keyboard::W:
						mw = true;
					break;
					case Keyboard::A:
						ma = true;
					break;
					case Keyboard::S:
						ms = true;
					break;
					case Keyboard::D:
						md = true;
					break;
				}
			}

			if(event.type == Event::KeyReleased) {
				switch(event.key.code) {
					case Keyboard::W:
						mw = false;
					break;
					case Keyboard::A:
						ma = false;
					break;
					case Keyboard::S:
						ms = false;
					break;
					case Keyboard::D:
						md = false;
					break;
				}
			}
		}

		//did we move the player?
		if(mw) {
			rany -= speed;
		}

		if(ma) {
			ranx -= speed;
		}

		if(ms) {
			rany += speed;
		}

		if(md) {
			ranx += speed;
		}

		//get the angle of the line between both player locations (zombie/player)
		angle 	= (atan2(rany - player1.sprite.getPosition().y, ranx - player1.sprite.getPosition().x) * 180) / M_PI;

		//steps between movement
		x 		= cos(angle) * speed;
		y 		= sin(angle) * speed;

		newx = player1.sprite.getPosition().x + x;
		newy = player1.sprite.getPosition().y + y;

		//move the sprites on the screen
		player1.sprite.setPosition(Vector2f(newx, newy));
		player2.sprite.setPosition(Vector2f(ranx, rany));

		window.clear();
		window.draw(player1.sprite);
		window.draw(player2.sprite);

		//update the screen
		window.display();		
	}

}

 

 

Pastie link: http://pastie.org/private/dfjwphmllmhnqdh0htyhdg


#1G4MR

Posted 07 January 2013 - 03:59 AM

So I attempted to do what you said and I had to make the speed the same for the player and the zombie.

 

Problem/Question 1: It seems I can't make both units on the screen different velocities (eg.: player1 = zombie with a speed of 2 and player2 = player with a speed of 3 [slightly faster]), if I do this and I go in a diagonal direction my player1 sprite will go in weird circular patterns unless I stop moving.

 

Problem/Question 2: It seems that if I allow diagonal movement (holding W and D or A & S, etc...) it will do this small curl up until I stop moving.  What's the best way to approach this problem?  

 

Problem/Question 3: There's also the issue when holding just down or right (S or D) and the sprite will just sit on the X or Y axis until I stop moving the unit on the screen and then proceed to move towards my unit.  How do I sort this?

Example:

Try what I'm talking about: http://morezombies.net/Zoms.zip (Virus Total Link: https://www.virustotal.com/url/1bb28babfa7e3049adc0f11e3f85c4a063728b799c20d4968e1c6c25921fc474/analysis/1357552685/)

Source:

 

#include "GameApp.h"

void GameApp::Setup() {

	//setup the game window
	window.create(VideoMode(width, height), "Zoms");

	//set framerate
	window.setFramerateLimit(60);

	player1.setSize(10, 20);
	player1.setPos(10, 10);

	player2.setSize(10,10);

	//lets say theg ames running now
	running 	= true;
}

void GameApp::Run() {

	//Setup game
	Setup();
	
	//used to get the current angle of the line between the two points
	//then we can properly move the sprite to the player
	double angle, x, y, newx, newy;

	//sprite speed 
	int speed 	= 2;

	//are the wasd buttons pressed?
	bool mw = false;
	bool ma = false;
	bool ms = false;
	bool md = false;

	//setup the player2 location
	int ranx 	= 300;
	int rany 	= 300;

	player2.setPos(ranx, rany);

	float timeprev, timecurr;

	while(window.isOpen()) {
		
		//loop through the active events (keyboard, mouse, etc...)
		while(window.pollEvent(event)) {

			if(event.type == Event::Closed) {
				window.close();
			}

			if(event.type == Event::KeyPressed) {
				switch(event.key.code) {
					case Keyboard::W:
						mw = true;
					break;
					case Keyboard::A:
						ma = true;
					break;
					case Keyboard::S:
						ms = true;
					break;
					case Keyboard::D:
						md = true;
					break;
				}
			}

			if(event.type == Event::KeyReleased) {
				switch(event.key.code) {
					case Keyboard::W:
						mw = false;
					break;
					case Keyboard::A:
						ma = false;
					break;
					case Keyboard::S:
						ms = false;
					break;
					case Keyboard::D:
						md = false;
					break;
				}
			}
		}

		//did we move the player?
		if(mw) {
			rany -= speed;
		}

		if(ma) {
			ranx -= speed;
		}

		if(ms) {
			rany += speed;
		}

		if(md) {
			ranx += speed;
		}

		//get the angle of the line between both player locations (zombie/player)
		angle 	= (atan2(rany - player1.sprite.getPosition().y, ranx - player1.sprite.getPosition().x) * 180) / M_PI;

		//steps between movement
		x 		= cos(angle) * speed;
		y 		= sin(angle) * speed;

		newx = player1.sprite.getPosition().x + x;
		newy = player1.sprite.getPosition().y + y;

		//move the sprites on the screen
		player1.sprite.setPosition(Vector2f(newx, newy));
		player2.sprite.setPosition(Vector2f(ranx, rany));

		window.clear();
		window.draw(player1.sprite);
		window.draw(player2.sprite);

		//update the screen
		window.display();		
	}

}

 

 

Pastie link: http://pastie.org/private/dfjwphmllmhnqdh0htyhdg


PARTNERS