Platformer scrolling is moving background, how to implement simple AI that works with this system

edited October 2016 in How To...

The question is a bit vague. Hopefully this will help.

Currently I am developing a Mario game and I am not using a camera tracking system that follows my mario object as I'm unaware of how to do this in Processing. Thus, when I move right - my mario position is always at x = 100 and my background image moves to the left which gives an illusion of movement.

This has worked as a great way to implement movement for Mario. However now I have an issue:

If you recall, when Mario hits a coin block, he can either spawn a mushroom powerup that has it's own AI or just a coin that gives you 200 points. This website might help refresh your memory if you need it

The following issue is arising because the world is all relative to Mario and his movement and I need to change this for these powerups/AI. I think visually this will explain it better: http://i.imgur.com/RpGIG6g.png

When I hit the coin block, it's creating a mushroom block at the position of the coin block. But because the world is relative to mario's position, when I move to the right/left - that mushroom itself follows left/right too. My first hurdle is to get the movement to not follow Mario.

Now, my choice is try make it independent of Mario or implement a camera system that follows Mario and thus makes everything else independent. I'm not really leaning to the latter because I'm amount 1000 lines in and this assignment is due in a couple of weeks.

If you want code, it's all here: https://github.com/chrisc96/Processing-Java-Projects/tree/master/ProcessingProjects/Game It's a bit daunting so if you need any explanations, do ask and I'll do my best to assist.

Chris

Answers

  • edited September 2016

    While implementing a camera is the elegant way, if you are invested in a world-offset approach instead you could simply create two variables / a vector called "worldX / worldY" (or just worldX if your screen height is fixed with no high climbing or deep falling).

    Then, as 'Mario' runs, you update the worldX / worldY offsets. Since everything else is computed with those offsets, everything has an absolute position in your sidescroller.

    So, if mario runs 3000 to the right hand hits a block, the mushroom spawns at objectX = 3000. Now run right to 3100 (when 'Mario' runs, the world moves instead). The object is at objectX=3000, so it is rendered 100 pixels to the left of Mario. If the mushroom slides left, it updates its position 3010, 3020, 3030 and eventually runs into Mario at 3050. etc.

    There are lots of examples, and some frameworks -- see @GoToLoop's link.

Sign In or Register to comment.