Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm using Cocoa2Ds to make a game for iOS. I know it's probably simple, but is there a way to change a CCFile's sprite images programmatically?
I created a hero sprite object and then dragged it into the main scene utilizing SpriteBuilder.
Ultimately, I would like to change the hero sprite object to another image that is animating (moving) as well.
You asked about changing a "CCFile" image, but I assume you meant "CCSprite" image. If so, changing a sprite image can be done by first creating a sprite frame and then assigning it to the sprite:
CCSpriteFrame * frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"NameOfFrame"];
[mySprite setDisplayFrame:frame];
For this to work the image you are referencing must already be loaded into memory, such as through a sprite sheet:
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"SpriteSheetFileName.plist"];
You mentioned animation. If you are trying to animate a sprite, and all the images are loaded into memory and they have the same name except for a sequential number appended to them, then you can have a sprite move through these images, thus animating it, as follows:
NSString * animateCycle = [NSString stringWithFormat:#"ImageName 00%%02d.png"];
The image names would be along the lines of "ImageName 0001.png", "ImageName 0002.png", and so on.
CCActionInterval * action = [CCAnimate actionWithSpriteSequence:animateCycle numFrames:8 delay:.1 restoreOriginalFrame:YES];
[mySprite runAction:action];
This will cycle through the images based on the designated delay.
I hope this helps.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to apply a sort of impulse onto a sprite and have it thrown onto the screen from the side. However, when it is after the screen I want the impulse to stop and for the regular physics to be applied to it, so I would be able to drag it around. Its difficult to explain but if anyone can help out it would be very much appreciated. Thanks!
Apple docs show that a we can influence the physicsBody of a sprite with these instance methods:
- applyForce:
- applyTorque:
- applyForce:atPoint:
- applyImpulse:
- applyAngularImpulse:
- applyImpulse:atPoint:
As for "throwing it on the screen from the side", you can have the sprite spawn outside your current skViews bounds, and have the force be applied to it accordingly (if the sprite is located outside the north side of the screen, you'll want to applyImpulse downward).
With respect to "regular physics" upon entrance of the skView, you can have a check in update:(CFTimeInterval)currentTime for those types of sprites entering the skViews frame OR define a protocol within your sprite's class, and have your SKScene subclass conform to that protocol; then have your sprite fire a method when entering a certain frame (skView.frame), which your SKScene will be able to respond to. You can then apply counter forces or impulses to that sprite.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm building a 2D game using SpriteKit and this is what I would like to achieve.
Imagine a vertically scrolling SKSpriteNode which represents a tall building. Building is represented using simple image and has a physics body set with + (SKPhysicsBody *)bodyWithTexture:(SKTexture*)texture size:(CGSize)size; (introduced with iOS 8) so it is closely following the building's path.
Some parts of the building are special. Colliding with those parts should be yielding a special collision action. For example, touching the wall of the building would fire an action 1 but touching any of the windows would fire an action 2.
What I haven't been able to do is in some way define those "special blocks" of the building.
I was thinking about making some kind of a "Collision Map" for each of the building's sprite images which would be basically a transparent image with non-transparent blocks determining a collideable parts of the building. Simple example shown bellow (left: building image, right: collision map image):
The problem with this approach is that when setting a SKPhysicsBody on a "Collision Map" image like the one above, the body is not applied to all blocks but it wraps around just one of those separate blocks. In other words: one physics body can be applied to only one, continuous block in image.
To conclude, I would like to know which approach are you using when determining non-continuous collision maps.
P.s.: building's SKSpriteNode is represented with multiple unique texture images which are scrolling vertically, one after another.
Thank you in advance.
Just an idea:
Can't you use two Sprites for the building which are positioned at the same place:
- one represents the physics body of your building (the left one from your image)
- invert your collision map image to get a single physics body block. The special areas should overlap the non special area by one pixel
Hope you understood what I mean. It's just an idea
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am interested in making path with finger (like connecting objects) between two elements. I am not sure how would I start on this.
I know that I could use Bezier path to create lines, but I am not sure how to create that line with finger. Does anyone have some good example?
I tried to google it, but I can't find anything like that made.
Thanks
I answered a question recently about slow/laggy performance on a similar setup. I.E. Drawing UIBezierPaths in CALayer. The answer contains a subclass of UIView which you can drop into a storyboard and will pretty much get you started. The header file is not shown in the answer, but it is literally a subclass of UIView (just add a UIView Subclass to your project). You should be able to copy the rest into your implementation file. Obviously you'll want to take out the performance testing code.
touchesMoved drawing in CAShapeLayer slow/laggy
If you simply want to Add a single line, you just need to get the starting point in touchesBegan, and build the path in touchesMoved. The commitCurrentRendering simply renders the touch points accumulated, then clears the UIBezierPath. This improves the performance as there wass a notable slowdown when UIBezierPath reached around 2000 points (touchesMoved will feed you a succession of points as your finger moves).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi, I am new to sprite kit framework I just wanted to know two things:
1) How to get all the SKSprite Nodes in a current scene?
2) How to make the application universal i.e it will run on iPhone 3.5", iPhone 4" and on iPad?
Thanks in advance.
in Scene.m, you can use self.children to get all the SKSprite Nodes
http://www.raywenderlich.com/49695/sprite-kit-tutorial-making-a-universal-app-part-1
While the second part of your answer is too broad, I can understand where you're coming from, as it's a bit overwhelming trying to figure out how to support all of the different screen sizes. But for other new developers who come across a similar overly broad question, if you are simply looking to have your scene fit in the different screen resolutions, and for the time being want to ignore the concept of multiple image resolutions and asset catalogs, one place to start is with handling the size of your scene and its scale mode:
In your viewController you could do something like this. It's a bit heavy handed, but designed to show the distinctions:
//we'll set what we want the actual resolution of our app to be (in points)
//remember that points are not pixels, necessarily
CGSize sceneSize = CGSizeMake(320, 568);
//check if this is an ipad, this means the screen has 2x the points
//it also means that the resolution is different than the iphone 5
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
sceneSize = CGSizeMake(sceneSize.width * 2, sceneSize.height * 2);
}
SKView *skView = (SKView*)self.view;
self.mainPrimaryScene = [[PrimaryScene alloc] initWithSize: sceneSize forSKView:skView];
//by setting the scale mode to fit, it takes the size of the scene
//and ensures that the entire scene is rendered on screen, in the correct ratio
self.mainPrimaryScene.scaleMode = SKSceneScaleModeAspectFit;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//you can check out all the other scale modes if you want to fill, etc
mainPrimaryScene.scaleMode = SKSceneScaleModeResizeFill;
}
[skView presentScene:mainPrimaryScene];
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Would someone be able to give me an example how to change the texture drawn when the user pressed the keyboard ?
Basically I have 5 sprite images stood still, up, down, left, right
My first attempt was to make boolean conditions in the update method such as if keys.left is pressed the rectangle moves left and for the draw method to draw the character moving left, my problem is the texture of him stood still doesnt disappear and overlaps the same when moving in different directions.
I've tried else statements etc but I'm stuck on basic movements.
So this is very pseudo code, but this is a basic approach how to do that
First we create class which contains all our informations about the player. You can add Health, Score etc aswell.
class Player
{
Texture2D Sprite;
Vector2 Position;
Vector2 Velocity;
static const float PlayerSpeed = 5;
}
Important here is the Position (top left of the sprite), the Velocity(the amount of change every second) and the sprite which is just our texture we want to use. Of course it would be better to use only one player texture and modify the source rect accordingly.
Now our input handling.
void OnKeyboard(GameTime aGameTime, KeyArg aKey)
{
if(aKey == Keys.Left)
{
mPlayer.Velocity = new Vector2(-Player.PlayerSpeed, 0);
mPlayer.Sprite = TextureManager.GetTexture("left_player");
}
else if(aKey == Keys.Right)
{
mPlayer.Velocity = new Vector2(Player.PlayerSpeed, 0);
mPlayer.Sprite = TextureManager.GetTexture("right_player");
}
mPlayer.Position += aGameTime.EllapsedMiliseconds * mPlayer.Velocity;
}
Here we just check which key was pressed, modify the velocity and change the current sprite for our player. The final line is the most important one, this modifies the players position using the velocity modified by the elapsed time of the frame. That way you have a steady movement instead despite any frame rate inconsistencies.
void Render()
{
Sprite.Draw(mPlayer.Sprite, mPlayer.Position);
}
Finally the rendering, how to render a sprite should be clear, here you just use the set sprite and the position.
There is a lot of room for improvements, for example minimizing texture switches, handling sprites with alpha, and the most important one proper handling of the keyboard. You need to steadily adjust the position, but depending on how you implement it, the movement might be bound to the key repeat rate, which may not be desired.