I have a Class for my Sprite and I'm trying to addChild from that class. The problem is it renders it and doesn't crash - I just can't see the image. I know the images position is right because created and added the child from the gameScene class and it worked and loaded.
I'm thinking this isn't working because its not subclass of SKscene that's why I added the main node from gameScene but didn't solve the problem.
I set up a "mainNode" node
keys is just the name of the sprite and its the class name
I know the image is being rendered its just not visible for some reason.
I know this because if I add no image name not "key_green" and change it to nothing. I get an console msg saying it can't find that image.
gameScene.m
#import "GameScene.h"
#import "Keys.h"
#implementation GameScene
#synthesize mainNode;
-(void)didMoveToView:(SKView *)view {
self.backgroundColor = [UIColor whiteColor];
mainNode = [SKNode node];
[self addChild:mainNode];
Keys * KeysOBJ =[[Keys alloc] init];
[KeysOBJ initKeys];
}
keys.h
#import <SpriteKit/SpriteKit.h>
#import "GameScene.h"
#interface Keys : SKSpriteNode{
SKSpriteNode * key1;
}
#property SKNode* mainNode;
-(void)initKeys;
-(void)loadKeys;
keys.m
#import "Keys.h"
#import "GameScene.h"
#implementation Keys{
}
#synthesize mainNode;
-(void)initKeys{
key1 = [SKSpriteNode spriteNodeWithImageNamed:#"key_red"];
key1.position = CGPointMake((self.frame.size.width)- (self.frame.size.width)+350, (self.frame.size.height)-(self.frame.size.height)+50);
[mainNode addChild:key1];
}
Your mainNode is nil in your Keys because you never set it before you call initKeys. This in theory will get it working.
Keys * KeysOBJ =[[Keys alloc] init];
KeysOBJ.mainNode = mainNode;
[KeysOBJ initKeys];
Also consider this
key1.position = CGPointMake((self.frame.size.width)- (self.frame.size.width)+350, (self.frame.size.height)-(self.frame.size.height)+50);
self doesn't have a size because you just called init and never supplied a texture or a size. Meaning your sprite location will likely be 350,50.
With that being said you are over complicating it. Your Keys shouldn't be a subclass of SKSpriteNode if you are never going to set a texture to it. It should be a SKNode.
Hopefully that makes some sense and gets you pointed back in the right direction.
Related
I'm using SpriteKit and Xcode 6 to develop a game. I have a single sks file that I want to use in all game scenes. If I add the emitter into each scene after I present it, the emitter stops in the old scene and stars new in the new scene, because it is created in each scene.
Therefore I tried to use a singleton class for creating the emitter only once and using it in all game scenes. I created a super class for all game scenes and create the emitter in that super class for all child scenes. But, what I tried to get with this steps is not working. the emitter still stops and starts new in every new scene, because it still created new in each scene.
How can I use the same emitter bei creating it only once and use it in all my game scenes whithout stoping and new starting the emitter?
Here my code. What I'm missing?
P.S. The same technique is working well with an audio player! http://www.galloway.me.uk/tutorials/singleton-classes/
//MyEmitterNode.h
#import <SpriteKit/SpriteKit.h>
#interface MyEmitterNode : SKEmitterNode
+(instancetype)sharedEmitterNode;
- (void)addEmitterNodeIntoScene:(SKScene *)scene;
#end
//--
//MyEmitterNode.m
#import "MyEmitterNode.h"
#interface MyEmitterNode ()
#property (strong, nonatomic) SKEmitterNode *mEmitterNode;
#end
#implementation MyEmitterNode
+ (instancetype)sharedEmitterNode {
static MyEmitterNode *sharedEmitterNode = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedEmitterNode = [[self alloc] init];
});
return sharedEmitterNode;
}
- (void)addEmitterNodeIntoScene:(SKScene *)scene {
self.mEmitterNode = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:#"MyParticle" ofType:#"sks"]];
[scene addChild:self.mEmitterNode];
}
#end
// -- MyScene: super classe for all game scenes
//MyScene.m
- (void)didMoveToView:(SKView *)view {
[[MyEmitterNode sharedEmitterNode] addEmitterNodeIntoScene:self];
}
// -- MyFirstScene: child class from MyScene
//MyFirstScene.m
- (void)didMoveToView:(SKView *)view {
[super didMoveToView:view];
// do other stuff
}
// -- MySecondScene: child class from MyScene
//MySecondScene.m
- (void)didMoveToView:(SKView *)view {
[super didMoveToView:view];
// do other stuff
}
I know this topic is covered in a hundred posts here, but I'm having a lot of trouble with this particular instance and cannot figure it out.
Basically, I'm using Spritebuilder to import sprites/nodes into my game. I import a sprite of some specific class in the body of the GameScene class, but I want to be able to define a variable inside of my sprite class, and then edit it from the GameScene class. For example, if my sprite collects a coin inside the GameScene, I want to change the speed of my sprite inside of the update method in the sprite class.
Below is my code, but unfortunately it isn't working. The variable increaseY and increaseX do not appear to be available in my GameScene class. I know this is because I didn't instantiate the Penguin class properly, but I don't know how to properly create an instance of this class while simultaneously importing the .ccbi file of it. The problem line is commented on and has a bunch of ** next to it to easily find it. It's in GameScene.m. I really appreciate the help, been stuck on this for several hours.
Penguin.h
#import "CCSprite.h"
#interface Penguin : CCSprite
{
float xPosition;
float yPosition;
}
#property (nonatomic,assign) float increaseY;
#property (nonatomic,assign) float increaseX;
#end
Penguin.m
#import "Penguin.h"
#implementation Penguin
#synthesize increaseX;
#synthesize increaseY;
- (id)init {
self = [super init];
if (self) {
CCLOG(#"Penguin created");
}
return self;
}
-(void) update:(CCTime)delta
{
self.position = ccp(self.position.x + increaseX,self.position.y + increaseY);
}
#end
GameScene.h
#import "CCNode.h"
#interface GameScene : CCNode
#end
GameScene.m
#import "GameScene.h"
#import "Penguin.h"
#implementation GameScene
{
CCPhysicsNode *_physicsNode;
CCNode *_catapultArm;
CCNode *_levelNode;
CCNode *_contentNode;
}
// is called when CCB file has completed loading
- (void)didLoadFromCCB {
self.userInteractionEnabled = TRUE;
CCScene *level = [CCBReader loadAsScene:#"Levels/Level1"];
[_levelNode addChild:level];
}
// called on every touch in this scene
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
[self launchPenguin];
}
- (void)launchPenguin {
// loads the Penguin.ccb we have set up in Spritebuilder
CCNode* penguin = [CCBReader load:#"Penguin"];
penguin.position = ccpAdd(_catapultArm.position, ccp(16, 50));
[_physicsNode addChild:penguin];
//THE FOLLOWING LINE DOES NOT WORK********************************
penguin.increaseY = 1;
// Gives Error------Property "increaseX" not found on object of type "CCNode *"
self.position = ccp(0, 0);
CCActionFollow *follow = [CCActionFollow actionWithTarget:penguin worldBoundary:self.boundingBox];
[_contentNode runAction:follow];
}
You have to change this line:
CCNode* penguin = [CCBReader load:#"Penguin"];
To this line:
Penguin* penguin = (Penguin*)[CCBReader load:#"Penguin"];
In the old line you were using the compiler was giving you an error because the CCNode class does not have a property called increaseX. increaseX is part of the Penguin class. If you want to access properties of the Penguin class you need to use a cast and let the compiler know, that what you are loading using the CCBReader is actually a Penguin instance.
I am new to programming and really need help now. I have been looking for an answer I think for the past two month. I'm using Xcode and objective-c. My question is about collision detection. There are thousands of example on what to do when 2 rectangle collide using CGRECT, such as alert or flip screen or play sound ext, but nothing anywhere about doing NOTHING lol! All I want is my object not going through the other object! That is all I want to keep dragging it on the screen. I just don't want the 2 object on top of each other and it seems like I'm the only online in the world that wants to do that because I can't find anything. So please help and since I'm new .. as simple as possible please so here :
#import "YellowDot.h"
#interface YellowDot ()
#end
#implementation YellowDot
#synthesize Dot;
#synthesize CollisionImage;
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *Drag = [ [ event allTouches ] anyObject ];
Dot.center = [ Drag locationInView: self.view ];
[self checkCollison];
}
-(void) checkCollison
{
if (CGRectIntersectsRect(Dot.frame, CollisionImage.frame))
{
AudioServicesPlaySystemSound(playSoundId);
}
}
- (void)viewDidLoad
{
NSURL *SoundURL = [ NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"beep"ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, & playSoundId);
[super viewDidLoad];
// Do any additional setup after loading the view.
}
and here is the .h file :
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#interface YellowDot : UIViewController
{
IBOutlet UIImageView *Dot;
IBOutlet UIImageView *CollisionImage;
SystemSoundID playSoundId;
}
#property (nonatomic, retain) UIImageView *Dot;
#property (nonatomic, retain) UIImageView *CollisionImage;
#end
So what could go in there ? It's already playing a sound when colliding as you can see but that's it. Dot is the image that I'm dragging around the screen and Collision image is the one that I want Dot to collide with but stop as a wall. Hope its clear enough ( I'm French, so sorry for the bad writing) :S Thank you.
Given that you're successfully detecting the collision, the answer would seem to be that if the move causes a collision, then don't update the object to the new position. Just don't update Dot.center. The sequence would be: get a touch event for the move, precompute the place where the object is going to be, if no collision, move it; if collision, don't update it's location.
Note that OpenGL might be better suited to this type of thing, given you're going to do a lot of it.
I'm getting the following warning from the line
self.physicsWorld.contactDelegate = self;
Assigning to 'id' from incompatible type 'PlayLevel *__strong'
-(id)initWithSize:(CGSize)size level:(int)level{
if (self = [super initWithSize:size]) {
_level = level;
self.physicsWorld.gravity = CGVectorMake(0,0);
self.physicsWorld.contactDelegate = self;
Not at all sure what's causing it.
This is what my header looks like:
#import <SpriteKit/SpriteKit.h>
#interface PlayLevel : SKScene
-(id)initWithSize:(CGSize)size level:(int)level;
#end
Any idea's what's causing it? I kind of need a contactDelegate. Thanks!
You should declarer on your class interface (inside <>) that your class implement the delegate.
you will have to add the delegate reference in your header file. For example if we are using the UINavigationController delegates we add <UINavigationControllerDelegate> in the header file. Similary you will have to add the contact delegate
In your header where you declare that PlayLevel extends SKScene, you need to also declare that it implements the SKContactDelegate interface like so:
#interface PlayLevel : SKScene <SKPhysicsContactDelegate>
The concept of being able to return to a previous scene from where you left off is briefly mentioned here: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/DesigningGameswithSpriteKit/DesigningGameswithSpriteKit.html
Other than that I can't find any more documentation on the subject. I know in other frameworks like Cocos2D you can pop scenes on a stack, and even have multiple scenes running at the same time.
How do I get this to work in SpriteKit.
I have a game with a swiping menu for character selection. Upon selecting a character the scene changes to another menu. I want users to be able to hit a back button and be presented with the previous scene, where the character they selected is in full view.
At the moment I have it presenting the previous scene as a new one. Which of cause creates it in a fresh state, with the first character in plain view, and not the character they selected.
This should be really simple, but for all my googling, I do not have a clue on how to implement this.
I don't know if anyone is still checking this, but I believe that I have another way to solve the problem.
I made a custom "PauseScene" and the user can set a return scene, meaning that when you are ready to go back, the PauseScene's view presents the return scene. Here's the code:
PauseScene.h
#import "MAScene.h"
#interface PauseScene : MAScene
#property (weak, nonatomic, readonly) SKScene * returnScene;
-(void)setReturnScene:(SKScene *)otherScene;
#end
PauseScene.m
#import "PauseScene.h"
#import "MASpriteButton.h"
#interface PauseScene ()
#property (strong, nonatomic) MASpriteButton * resumeButton;
#end
#implementation PauseScene
-(void)setReturnScene:(SKScene *)otherScene
{
_returnScene = otherScene;
}
-(void)didMoveToView:(SKView *)view
{
[self createContent];
}
-(void)createContent
{
__weak typeof(self) weakMe = self;
MASpriteButton * resume = [[MASpriteButton alloc] initWithBackgroundImageNamed:#"cardback.png" andSelectedImageNamed:#"cardback.png" andCallbackBlock:^{
NSLog(#"removing...");
[weakMe.view presentScene:self.returnScene];
}];
resume.size = CGSizeMake(self.widthToHeightRatio * 20, self.heightToWidthRatio * 20);
resume.position = CGPointMake(self.size.width/2, self.size.height/2);
self.resumeButton = resume;
[self addChild:self.resumeButton];
[self setBackgroundColor:[UIColor blueColor]];
}
#end
The way I used it was that when the user hits a pause button in the gameplay scene, I call these lines:
self.paused = YES;
PauseScene * pScene = [[PauseScene alloc] initWithSize:self.size];
[pScene setReturnScene:self];
[self.view presentScene:pScene];
Note that self in this little block is the gameplay scene.
By having the pause scene keep a weak pointer to the gameplay scene, it can keep a pointer to the return scene without dealloc-ing it when the pause scene is dealloc-ed.
PS The MAScene class is just a little extension of the SKScene class, I just added a couple things to it. If anyone wants that too just let me know.
Assuming the menu scene is also implemented in Sprite Kit, you could create a modal view controller, present it, and put the Sprit Kit scene over that modal view.
So, specifically, create a new UIViewController inheriting class MenuViewController, and a new SKScene inheriting class, MenuScene. The class 'MenuScene' MenuScene should be the Scene you're looking to present. Hook up MenuScene and MenuViewController like you would normally hook up an SKScene and its view controller. Make sure you have the original scene's view controller as a property of the original SKScene.
Wherever you want to present this menu, you can call from the original SKScene:
MenuViewController *modalViewController = [[MenuViewController alloc] init];
[self.viewController presentModalViewController:modalViewController];
There are simpler ways to just transition b/w SKScene instances, but if you want to keep the intial SKScene running in the background, this is what you would have to do, I believe.
You just should keep strong pointer to the leaving scene before you call new
Here is example of realisation via singleton (actually usual singleton with scene pointer, no magic)
#interface Global : NSObject
+ (Global*)sharedInstance;
#property (strong, nonatomic) SKScene *mainScene;
#end
And .m file
#implementation Global
+ (instancetype) sharedInstance{
static Global *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[Global alloc] init];
});
return _sharedInstance;
}
#end