Thread: Character moving algo

  1. #1
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540

    Simplier way of doing this

    I feel like I am posting too much in this board; I appreciate you guys graciously dealing with my ignorance. Anyway I am stumbling around with this DirectX thing and like you have all said it is easier than it first appeared, but alas to the point of this post.

    In order to learn DirectX I have decided to remake a simplified version of the 8 bit NES Mario brothers game. As all of you undoubtedly remember Mario can do a lot of things like move left and right, jump left and right, and squat facing left and right. My code is currently getting really sloppy when it is coming to selecting the correct sprite to render to the screne and I was hoping someone might have a better way of doing things, or at least point me in the right direction.

    Currently I have loaded all the mario sprites into a structure that is defined as such:

    Code:
    struct _marioSprite {
    		_Sprite Mario[10];
    		int xPos, yPos, currentMario;
    		D3DXMATRIX d3dMario;
    		bool bBig;
    }marioSprite;
    Where currentMario is an integer between 0 and 9 which selects which sprite gets rendered. Now the "fun part" starts here with the slop you can kind of call a function that takes the message and WPARAM from the message loop:

    Code:
    void marioEngine::marioMove (WPARAM wParam, UINT message) {
    
    	static int vector;
    	static int offset;
    
    	switch(wParam) {
    
    		case VK_LEFT:
    			vector = -1;
    			offset = 5;
    			break;
    		case VK_RIGHT:
    			vector = 1;
    			offset = 0;
    			break;
    		case VK_DOWN:
    			marioSprite.currentMario = offset + 4;
    			marioSprite.yPos = 1300;
    			break;
    	}
    	if(message == WM_KEYUP) {
    		if(wParam == VK_DOWN)
    			marioSprite.yPos = 1240;
    		marioSprite.currentMario  = 0 + offset;
    	}else if(marioSprite.currentMario == (0 + offset)){
    		marioSprite.currentMario++;
    		marioSprite.xPos = marioSprite.xPos + (1 * vector);	
    	}else if(marioSprite.currentMario == (1 + offset)) {
    		marioSprite.currentMario++;
    		marioSprite.xPos = marioSprite.xPos + (2 * vector);
    	}else if(marioSprite.currentMario == (2+offset)){
    		marioSprite.currentMario--;
    		marioSprite.xPos = marioSprite.xPos + (2 * vector);
    	}
    	_bRedraw = true;
    }
    Now vector holds whether or not Mario moves right or left and is set under the VK_RIGHT and VK_LEFT cases above. Offset represents the number of places to move in the mario array to switch between right and left facing sprites. As you can see when you press left offset becomes 5 which starts you off at left facing sprites. The whole
    Code:
    else if(marioSprite.currentMario == (1 + offset)) {
    		marioSprite.currentMario++;
    		marioSprite.xPos = marioSprite.xPos + (2 * vector);
    	}else if(marioSprite.currentMario == (2+offset)){
    		marioSprite.currentMario--;
    		marioSprite.xPos = marioSprite.xPos + (2 * vector);
    thing simply switches between the 2 running sprites I have and causes the animation effect. I am currently removing the hardcoded values (1240 and 1300). They were simply because a sqatting mario is smaller than a standing mario and mario still needed to touch the ground. I am just going to lengthen my sprite vertically to get around the adjusting yPos thing.


    As you can see the code is pretty hairy and introduces alot of room for bugs. Any comments or pointers will be greatly appreciated.

    And of course Happy Holidays
    Last edited by andyhunter; 12-21-2004 at 10:45 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well you need to look into using vectors or arrays.

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    vector as in D3DXVECTOR, or vector as in move right/left. I am afraid I am just not quite following. Thankyou for entertaining my ignorance.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Vectors as in arrays. I just recently learned how to use them, and they are handy.

    http://cplus.about.com/od/beginnerct.../aa050102a.htm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  4. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  5. Moving a character to stdin?
    By Geolingo in forum C Programming
    Replies: 2
    Last Post: 09-27-2003, 06:37 PM