How to make a jump sprite - xna

I'm building a Mega Man clone for a school project and I just finished the code to make my character jump and it works perfectly. But I need it to display the actual jump sprite of Mega Man. This is the whole code in Game1:
http://pastebin.com/py7Y7mtD
The AnimatedSprites jt helps you decide the direcrtion and display the sprite. Doesn't matter right now if it's efficient. It's just for starts. When I'll get to more complex stuff I'll work on it.
MegamanJump has the jump sprite(5 or 6 sprites displayed from left to right)
MegamanJump_Flip is the same thing but to the left(again, forget about efficiency).

Basically you want to switch out the current drawable sprite with the sprite you want to draw depending on moving direction or moving direction + jump.
Since you already have a running update method, all you have to do is write another method that does this for you, a state variable to track what is happening and the actual states it can have, preferrably in an enum for clarity.
Essentially
private void swapSprite() {
if(state == CharacterState.LEFT) {
sprite = leftDrawable;
}
//etc other single ifs
if(state == CharacterState.LEFTJUMPING) {
sprite = leftJumpDrawable;
}
}
Change the states on button press combinations, call this method after checking for movement.
I hope this helped!

Related

Detecting touch pressure (force) with swift (SpriteKit)

So, I've got this game with Swift on Xcode where I have a SKShapeNode that jumps. I would like, though, to have this jump commensurated to the touch the player does. Hence, a small tap will get a small jump and a more brisk one gets a higher jump. How is it possible to do this (mind that I do not want any 3D touch options)?
Usually, this sort of thing in games is done by timing the press and altering the jump height accordingly. (Which is also why using physics in 2D platforming games is tricky for this kind of thing).
The way it is done in games like Super Mario is to have the start of the jump movement begin as soon as the touch is pressed. Then after some amount of time (as the character is half way "up" the jump) you check to see if the touch is still down. If it is you can make the jump animation continue upwards. If the touch is released then start to bring the jump down again.
That way a short touch is a small jump but a longer touch is a higher jump.
If you have a smallJumpAction and bigJumpAction at the moment then what you could do is break it down into smaller parts...
beginJumpAction which is triggered immediately and then... smallJumpEndAction and bigJumpEndAction.
This could be done with a sequence action...
let beginJumpAction = //move the player upwards slightly
let smallJumpEndAction = //begin to move the player down
let bigJumpEndAction = //move the player up a bit more and then move down
let blockAction = SKAction.run {
if touchStillDown {
player.runAction(bigJumpEndAction)
} else {
player.runActgion(smallJumpEndAction)
}
}
player.runAction(SKAction.sequence([beginJumpAction, blockAction])
Something like this anyway. There are many ways to approach this though :)

How to make player-sprite "jump" using SpriteKits built-in physics engine

I'm a little confused trying to find a reliable way to make my player-sprite jump when the user taps the screen.
So far I've been doing this:
if(self.physicsBody.velocity.dy == 0.0 && _shouldJump) {
[self.physicsBody applyImpulse: CGVectorMake(0.0, 195.0);
}
Which works, but I'm looking for something more reliable and better. Specifically, right now, the user cannot jump when he's sliding down a slope, or on the edge of a block about to fall down (because .dy isn't exactly 0). I'd like the player to be able to jump in these cases too.
I've tried something like .dy < 5.0 && .dy > -5.0, but then he's able to jump when he's at the top of his current jump, making him jump even higher.
Is there a way of checking which physicsBodies my player-sprite is in contact with maybe? Or do you have any other suggestions?
I have read Ray Wenderlich's tutorial on how to create a Platform Game Like Super Mario, but they're building their own custom Physics Engine. I'd like to stick with the one built into SpriteKit, mainly becuase of the collision handling and detection.
EDIT 1:
Bear in mind that I cannot set a flag _isOnGround = YES every time the Player touches the ground in didBeginContact, as he is bouncy and will bounce up-and-down a few times before coming to a stop. I'd prefer the user to be able to jump as soon as he touches the ground when he's holding the screen, but this is not necessary.

performSelectorinBackground in certain time

I'm new to iOS programming and I made a little game.
I have some sprites in the background of my scene which I want to move slowly from left to right.
Right now I'm doing this in the MainScene:
[self performSelectorInBackground:#selector(doBackgroundAnimation:) withObject:sprite];
and the method in which the animation takes place looks like this:
-(void)doBackgroundAnimation:(CCNode *)sprite {
while (//sprite is still in the scene) {
//move sprite a little bit right
}
}
This solution is not really what I was looking for, since the time it takes to run through the while-loop varies from device to device.
Is there a way to make sure that the animation takes an exact amount of time e.g. 5 seconds?
Something like:
performSelectorInBackground:#selector(doBackgroundAnimation:) withObject:turtle inTime:5.0
Thank you for your help!
It's been a while since I did game stuff but you have to adjust the distance you move the sprite according to the time since the last update of the screen. So you have a var called speed that you * by the time since the last refresh and then move the sprite that distance.
There are methods in sprite kit that deal with this. There are lots of tutorials on YouTube that cover this.

Sometimes sprite is pushed away when touches another sprite

I am making a game using cocos2d and SpriteBuilder. There is a hero sprite and coins, that should be collected by him. That's a horizontal scrolling game, so every time hero touches a coin, this coin changes it's x-coordinate 2000px to the right and new y-coordinate is generated at random. Using update method I move it to the visible area as a "new" coin. But when hero flyies by and doesn't collect it ,coin must change coordinates only when it's already off the screen, so I tried this solution:
-(void)update:(CCTime)delta{
_coin.position=ccp(_coin.position.x - delta * scrollSpeed, _coinY);
if (CGRectIntersectsRect(_hero.boundingBox,_coin.boundingBox)) {
_coinY=arc4random() % 801 + 100;
_coin.position=ccp(_coin.position.x + 2000.f,_coinY);
}
else if(_hero.position.x >= _coin.position.x + 150){
_coinY=arc4random() % 801 + 100;
_coin.position=ccp(_coin.position.x + 2000.f,_coinY);
}
It works,but after that I found a small bug(I am not sure, whether it's related to this code) : sometimes, when hero touches coin, hero is like pushed away to the left. I have no idea why.
How to fix it?
Is that way, that I am using for coin ,right?
I see 2 issues with your approach:
1. Hero bounces of the coin.
To fix this you need to make your coin a sensor. This way you will still be notified about collisions, but the hero will simply pass through the coin without actually hitting it.
I'm not sure if you can set this in SpriteBuilder, but probably you can enumerate all coins after loading the scene and set sensor property to YES:
coin.physicsBody.sensor = YES;
Things like this is one of the reasons I believe you first need to learn pure Cocos2D and only then use tools making your life easier such as SpriteBuilder. Which is a great tool, but in some cases you still need know what happens behind the scenes.
2. You're mixing physics with things like CGRectIntersectsRect
If you're using physics engine you need to detect collisions via collision delegate and not by checking CGRectIntersectsRect in update:.
It is hard to explain how to do this in a few sentences, so here is a nice tutorial that shows how to detect collisions in Cocos2D v3 and of course there is a chapter about this in my book.
By the way you shouldn't use update:at all when manipulating physics nodes, use fixedUpdate: instead.
I hope this will help you to solve your issue.

Actionscript 2 - hitTesting multiple instances

I'm making a platformer using Actionscript 2. I got the jumping and gravity and everything going, but I'm limited to one "ground". As one might imagine, platformers require multiple "grounds" for jumping onto and falling off of. I tried editing the "ground" movie clip so that it was several blocks with nothing between them, but when I try this, my character will jump, land at the peak of its jump, and repeat the process until it lands on the top of the ground movie clip, then behave regularly, jumping and landing on top of the ground movieclip, even if there's no part of the ground movieclip to land on.
Here's my ground-gravity code.
if(this.hitTest(_root.ground))
{
this._y -= gravity;
gravity = 0;
jumping = false;
}
When you use hitTest between two MovieClips, it doesn't matter if there's nothing inside the movieclip, as long as the hittesting movieclip is INSIDE the bounding box (the blue outer rectangle of a movieclip when you select it) of the other movieclip.
To solve your problem, but still have many grounds in the same MovieClip, you have to use the second usage of hitTest, hitTest(x, y, shapeFlag) -- this will not check if two movieclips are hittesting each other, but rather if a MovieClip is touching a POINT (just an x and a y coordinate). But the real advantage of using this is the third parameter, shapeFlag, which is a Boolean value (either true or false) -- if it's false, then the hitTest will take the whole movieclip into account, meaning that even if there are empty spaces, those will be included in the hitTest, WHILE, if it's true, then only the actual shape of the MovieClip will be considered during the hitTest.
First of all, you have to get inside your Character's MovieClip and move him so that the registration point of the movieclip (you know that small plus sign), is at the BOTTOM of your character. Then, change your code to this:
if(_root.ground.hitTest(this._x, this._y, true))
{
this._y -= gravity;
gravity = 0;
jumping = false;
}
This will check if the SHAPE of the ground MovieClip (meaning only the platforms, NOT the empty spaces between them) are touching the registration point of your MovieClip, which is located at the bottom, at the feet, of your character. So, whenever the feet of your character are touching any of the platforms inside the ground movieclip, the code will be executed and your character wil stop.
Hope this helps :)

Resources