So I create a delegate protocol and corresponding property in my scene. In my view controller that holds all my scenes I set the scenes delegate to itself so I can call a fullscreen ad(from the view controller) in the menu which is the first game scene called. This works great I can call the method. But after i transition to the level scenes then back to the menu scene calling my method to show fullscreen ad does nothing. Im assuming because the delegate being set to itself has been lost. How can I set the delegate back to itself within the game scene? This is how I do it inside the View controller.M
myScene = [GameScene unarchiveFromFile:#"GameScene"];
((GameScene *)myScene).mySceneDelegate = self; // i need to be able to do this within the scene
myScene.scaleMode = SKSceneScaleModeResizeFill;
// Present the scene.
[skView presentScene:myScene];
How I call the fullScreen method thats in my VC from my menu scene...
[self.mySceneDelegate showFullScreen:self];
...so in my code when I transition from my LevelOne scene I try to reassign the delegate with the following code but it nothing happens
-(void)goBackToMenu{
GameScene *newscene = [GameScene sceneWithSize:self.size];
[self.view presentScene:newscene transition:[SKTransition doorsCloseHorizontalWithDuration:1]];
((GameScene *)newscene).mySceneDelegate = self;
}
I have also tried setting the property attribute to Strong instead of weak with no luck either
**********************ALL CODE **************************************
More code.. my Viewcontroller.h
#protocol TCAMySceneDelegate;
#interface GameViewController : UIViewController<TCAMySceneDelegate>
#property(nonatomic, weak) id<TCAMySceneDelegate> mySceneDelegate;
#end
my view controller.m
- (void)viewDidLoad
{
[super viewDidLoad];
SKView * skView = (SKView *)self.view;
myScene = [GameScene unarchiveFromFile:#"GameScene"];
((GameScene *)myScene).mySceneDelegate = self;
// Present the scene.
[skView presentScene:myScene];
}
-(void)showFullScreen:(GameScene *)gameScene{
NSLog(#"Show Full Screen");
[revmobFS loadWithSuccessHandler:^(RevMobFullscreen *fs) {
[fs showAd];
NSLog(#"Ad loaded");
} andLoadFailHandler:^(RevMobFullscreen *fs, NSError *error) {
NSLog(#"Ad error: %#",error);
} onClickHandler:^{
NSLog(#"Ad clicked");
} onCloseHandler:^{
NSLog(#"Ad closed");
}];
[RevMobAds startSessionWithAppID:#"547251235f2a043608a66f1a"
withSuccessHandler:^{
NSLog(#"Session started with block");
// Here, you should call the desired ad unit
revmobFS = [[RevMobAds session] fullscreen];
[revmobFS showAd];
// [RevMobAds session].testingMode = RevMobAdsTestingModeWithAds;
revmobFS.delegate = self;
[revmobFS loadAd];
} andFailHandler:^(NSError *error) {
NSLog(#"Session failed to start with block");
}];
}
my game scene.h file..
#protocol TCAMySceneDelegate;
#interface GameScene : SKScene
#property (nonatomic, weak) id<TCAMySceneDelegate> mySceneDelegate;
#end
#protocol TCAMySceneDelegate <NSObject>
-(void)showFullScreen:(GameScene *)gameScene;
#end
my game scene.m file..
-(void)didMoveToView:(SKView *)view {
//Show Ad
[self.mySceneDelegate showFullScreen:self];
}
now the above showFullScreen: is called but online when the gameScene (my game menu) is presented the first time .. after transitioning to other scenes in the games such as my levels then go back to my menu(gamescene) it never gets called. Hope this is more clear now
how i switch to another scene from my game scene.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
NSLog(#"Level Selected: %#", node.name);
int x = [node.name intValue];
switch (x) {
case 1:{
LevelOne *newscene = [LevelOne sceneWithSize:self.size];
[self.view presentScene:newscene transition:[SKTransition doorsOpenVerticalWithDuration:1]];
break;
}
case 2:{
LevelTwo *newscene = [LevelTwo sceneWithSize:self.size];
[self.view presentScene:newscene transition:[SKTransition doorsOpenVerticalWithDuration:1]];
}
break;
}
case 3:{
LevelThree *newscene = [LevelThree sceneWithSize:self.size];
[self.view presentScene:newscene transition:[SKTransition doorsOpenVerticalWithDuration:1]];
}
break;
}
//..........
default:
break;
}
}
LevelOne.h code...
#import <SpriteKit/SpriteKit.h>
#import "DrawCanvas.h"
#import "GameScene.h"
#interface LevelOne : SKScene<SKPhysicsContactDelegate>
#end
Personally, I found your description as well as the code you posted a bit lacking in details as well as being a bit confusing. When providing code you will probably get better/quicker help if you provide more detail. For example, which file does goBackToMenu belong to?
How/in what context is
[self.mySceneDelegate showFullScreen:self];
called? I would assume that it is from some trigger point in the GameScene. And what is the method signature of showFullScreen?
And since you don't indicate what type mySceneDelegate is, you're making the readers guess what you mean.
So trying to decipher what you are asking...
Let's assume you have some delegate which is called GameSceneDelegate. I am also going to assume you already know SKSceneDelegate exists and are either using it already but just haven't posted that code or don't need to use it.
Your GameViewController would have it's interface defined as:
#interface GameViewController : UIViewController<GameSceneDelegate>
And your GameScene would have a property defined like this:
#property (nonatomic, weak) id<GameSceneDelegate> mySceneDelegate;
Note you should be using weak here. Because if your GameViewController ever has a strong reference to your GameScene, then you've just created a retain cycle.
You have a few options of when to set mySceneDelegate. If I know I must have a delegate defined, what I often do is add it as an argument to the initializers. This way it is clear that it must have a delegate. So, given you are using class methods to generate instances, you'd do something like this:
#interface GameScene : SKScene
#property (nonatomic, weak) id<GameSceneDelegate> mySceneDelegate;
+ (instancetype)unarchiveFromFile:(NSString *)file mySceneDelegate:(id<GameSceneDelegate>)mySceneDelegate;
+ (instancetype)sceneWithSize:(CGSize)size mySceneDelegate:(id<GameSceneDelegate>)mySceneDelegate;
#end
And you those methods would look like:
+ (instancetype)unarchiveFromFile:(NSString *)file mySceneDelegate:(id<GameSceneDelegate>)mySceneDelegate
{
GameScene *scene = [super unarchiveFromFile:file];
scene.mySceneDelegate = mySceneDelegate;
return scene;
}
+ (instancetype)sceneWithSize:(CGSize)size mySceneDelegate:(id<GameSceneDelegate>)mySceneDelegate
{
GameScene *scene = [super sceneWithSize:size];
scene.mySceneDelegate = mySceneDelegate;
return scene;
}
Note to make this work, you'll need to define this in the GameViewController.h (or a separate file for the category if you wish)
#interface SKScene (Unarchive)
+ (instancetype)unarchiveFromFile:(NSString *)file;
#end
The alternative is to immediately set mySceneDelegate right after creating your instance of the scene.
Realistically, if you are doing this, it should work and your delegate's should stay in tact. FWIW, I am actually doing this in my game. So I know it works.
Okay, so this is where things are hazy in your question. You indicate the delegate is "lost". The only way the delegate can be "lost" would be if someone explicitly sets it to nil. Or the other possiblity I suppose is your GameScene is being dealloc'd and you don't realize it.
I'd setup two things to help debug it.
In GameScene add
- (void)dealloc
{
NSLog(#"I'm being dealloc'd");
}
- (void)setMySceneDelegate:(id<GameSceneDelegate>)mySceneDelegate
{
_mySceneDelegate = mySceneDelegate;
NSLog(#"Setting mySceneDelegate");
}
So now you can try and determine where it is "getting lost". If any of those print out to the console at the time it is lost, simply set a breakpoint in those methods to see who is calling it.
One other pieces of info that would also help are
What do you mean assigning a delegate to itself? I have no idea what
that means.
You mentioned you assume the delegate is lost. Why? Do you have
evidence or is this purely a guess?
If you can provide some answers and better detail, we can probably figure out what is going awry.
Okay, now looking at your code, I think your problem is here in GameScene.m:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
NSLog(#"Level Selected: %#", node.name);
int x = [node.name intValue];
switch (x) {
case 1:{
LevelOne *newscene = [LevelOne sceneWithSize:self.size];
newScene.mySceneDelegate = self.mySceneDelegate;
// Or, if you can make it work, newscene = [LevelOne sceneWithSize:size mySceneDelegate:self.mySceneDelegate;
[self.view presentScene:newscene transition:[SKTransition doorsOpenVerticalWithDuration:1]];
break;
}
case 2:{
LevelTwo *newscene = [LevelTwo sceneWithSize:self.size];
newScene.mySceneDelegate = self.mySceneDelegate;
// Or, if you can make it work, newscene = [LevelOne sceneWithSize:size mySceneDelegate:self.mySceneDelegate;
[self.view presentScene:newscene transition:[SKTransition doorsOpenVerticalWithDuration:1]];
}
break;
}
case 3:{
LevelThree *newscene = [LevelThree sceneWithSize:self.size];
newScene.mySceneDelegate = self.mySceneDelegate;
// Or, if you can make it work, newscene = [LevelOne sceneWithSize:size mySceneDelegate:self.mySceneDelegate;
[self.view presentScene:newscene transition:[SKTransition doorsOpenVerticalWithDuration:1]];
}
break;
}
//..........
default:
break;
}
}
What I am doing here is assigning the the new scene's mySceneDelegate to the existing one being used.
You can clean this code up to be more readable.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
NSLog(#"Level Selected: %#", node.name);
int x = [node.name intValue];
GameScene *newScene = nil;
switch (x) {
case 1:
newScene = [LevelOne sceneWithSize:self.size];
// Or, if you can make it work, newScene = [LevelOne sceneWithSize:size mySceneDelegate:self.mySceneDelegate;
break;
case 2:
newScene = [LevelTwo sceneWithSize:self.size];
// Or, if you can make it work, newScene = [LevelTwo sceneWithSize:size mySceneDelegate:self.mySceneDelegate;
break;
case 3:
newScene = [LevelThree sceneWithSize:self.size];
// Or, if you can make it work, newScene = [LevelThree sceneWithSize:size mySceneDelegate:self.mySceneDelegate;
break;
// other cases..........
default:
break;
}
if (newScene) {
newScene.mySceneDelegate = self.mySceneDelegate;
[self.view presentScene:newScene transition:[SKTransition doorsOpenVerticalWithDuration:1]];
}
}
For your levels, you would need to do the following:
#import <SpriteKit/SpriteKit.h>
#import "DrawCanvas.h"
#import "GameScene.h"
#interface LevelXYZ : GameScene<SKPhysicsContactDelegate>
#end
Where XYZ is the level (eg. One). By doing this, you inherit mySceneDelegate from GameScene. If for some reason you are insistent that the level scenes are not inheriting from GameScene, then add a base class which holds the delegate. So that would be something like:
#interface MyBaseScene : SKScene
#property(nonatomic, weak) id<TCAMySceneDelegate> mySceneDelegate;
#end
And then GameScene would be this:
#interface GameScene : MyBaseScene
And your level scenes would be
#interface LevelXYZ : MyBaseScene<SKPhysicsContactDelegate>
Note a couple of things I did.
Changed newscene to newScene. In iOS, you typically use came case.
you were doing it for mySceneDelegate. Consistency is key.
Originally declared newScene as nil and assigned as need. Then at the end of the switch, if a newScene exists, assign the delegate and
present. This way, you are only doing it in one place. It makes life
easier. For example, if you wanted to change the duration for each
one, then what? You have to change ever case. Note there is a
downfall to this. If you wanted on level to have a special duration,
then it doesn't quite work. But you can change it by making the
duration and transition type variables.
BTW, I would strongly advise against having your scene class contain the code that does the actual transition. I would either have it call a delegate to do it or find another way (like using a notification that a scene change is needed). For example, in my current game, I have my view controller be the controller of all scene changes. The advantage of this is you can also call scene transitions from other external forces (and not have your scene be the one that manages it).
Lastly, you can hopefully see that the only way the issue was discoverable was by presenting the right code. I will agree it is hard to know what code to present, because if there is too much code, then people may not read it. It's a fine line, but some food for thought.
Related
I have to place functionality where CCButton can be dragged to proper position, wherever user needs to place them.
I have create a custom class for this but the issue is click method of the button is not being called when user clicks on the button.
touchyButton.h
#import "cocos2d.h"
#import "cocos2d-ui.h"
#interface touchyButton : CCButton { ... }
#property (nonatomic, assign) BOOL touchMoved;
#end
touchyButton.m
#import "touchyButton.h"
#implementation touchyButton
- (void) touchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
NSLog(#"touchMoved...", nil);
self.touchMoved = YES;
self.anchorPoint = ccp(0.5, 0.5);
CGPoint touchLoc = [touch locationInNode:self.parent];
//CGPoint inTouchLoc = [self convertToNodeSpace:self.anchorPoint];
//CGPoint touchP = ccpAdd(touchLoc, inTouchLoc);
//self.position = [self.parent convertToNodeSpace: touchP];
self.position = touchLoc;
}
- (void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
self.touchMoved = NO;
NSLog(#"touchEnded...", nil);
}
#end
As the code explains, we are just trying to move the button wherever on the screen when user drags on the button.
Calling of the button in main code where it needs to be displayed.
touchyButton *btnRight = [touchyButton buttonWithTitle: #"" spriteFrame:[[CCSprite spriteWithImageNamed: #"arrR.png"] spriteFrame]];
[btnRight setBackgroundOpacity:0.5f forState: CCControlStateNormal];
[btnRight setAnchorPoint: ccp(1, 0.5)];
[btnRight setPosition: ccp(viewS.width - 10.f, viewS.height/2)];
[self addChild: btnRight];
[btnRight setTarget:self selector: #selector(performRightJump:)];
Now, when user clicks on the button, the button goes into selected state but performRightJump never fires. Can anyone suggest any alternative how I can implement the button with dragging behaviour with target action working..? Any hint would be appreciated as well.
One more thing is, in current code I can only be able to move the button's anchor point to the new touch point. Any idea how I can move the button in real fashion? The current method causes problem of tapping of first time for move, button's anchor point jumps to the tapped point.
this code is cocos2dx but useful for you
create a new class classA and classB
add this code
classA.h
{
//here implement
CC_SYNTHESIZE_RETAIN(classB *, _ classB, _ ClassB);
}
classA.cpp
bool classA::init()
{
set_classB(classB::initwithclass());
this->addChild(get_ClassB(),0);
//here create button
return true;
}
bool classA:: onTouchBegan (Touch *touch ,Event *event)
{
_classB->setposition(touchLoc);
return true;
}
void classA:: onTouchMoved(Touch *touch, Event *event)
{
}
void classA:: onTouchEnded(Touch *touch ,Event *event)
{
}
//as well as this code use in cocos2d
I'm trying to pass an object from an SKScene to the current UIViewController where the scene is being shown, it's like I created a label that will only be triggered once the object from the scene reached a specific location, I understand that I can just easily create a SKLabel and have it added to the scene once the object reaches the location I want it to, but I'd rather do it the ViewController style way since I'll be adding a lot of objects that will do the same thing as my app Progress, that reason step aside, I did actually tried adding an sk label to see if it will work that way, Yes I was able to see the SKLabel appear upon the object reaching let's say location.x = 50 and I set the node to be removed when the object reaches location.x = 270, But the problem is it's only doing it once, after the object being added again, it seems that the scene is not removing the node even though I'm pointing my object to hit 270..
By the way, since I mentioned 2 problems, here's the code that executes the said operation for the SKlabel node which is only happening once, I want it to execute the statement one time, everytime I hit that location
if (newLocation.x==270.00 )) {
[self addingTheLabel];
}
if (newLocation.x == 50.00) {
SKAction *removingTheNode = [SKAction removeFromParent];
[self.label runAction:removingTheNode];
}
Nevermind, was able to resolve the issue now..
For those people who might encounter this,Creating a protocol on your scene will fix the issue:
#protocol gameSceneDelegate <NSObject>
-(void)testProtocol;
-(void)testProtocol2;
#end;
#interface MyScene : SKScene
#property (weak,nonatomic) id<gameSceneDelegate> delegate;
Implement it on your scene's view controller:
#interface ViewController : UIViewController<gameSceneDelegate>
in ViewController.m you need to set first your scene as your delegate:
MyScene *scenePointer = (MyScene*) scene;
[scenePointer setDelegate:self];
and finally, implement the methods on your ViewController:
-(void)testProtocol{
NSString *sampleString2 = [[NSString alloc]initWithFormat:#"This will show when testProtocol is selected"];
self.sampleLabel.text = sampleString2;
}
-(void)testProtocol2{
NSString *sampleString3 = [[NSString alloc]initWithFormat:#"This will show when test 2 protocol is selected"];
self.sampleLabel.text = sampleString2;
}
Make an if statement inside your ViewDidLoad that if your scenePoint is the delegate do the following:
if([scenePointer delegate]){
[self testProtocol];
[self testProtocol2];
}
Now, Going to your Scene, since what I want is for the label to change whenever the SpriteNode hits a specific location, what I did is:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
for(UITouch *touch in touches){
CGPoint location = [touch locationInNode:self];
CGPoint newLocation = CGPointMake(location.x, 450);
if (newLocation.x == 270) {
//This will trigger the method everytime the spritenode hit's this location
if([_delegate respondsToSelector:#selector(testProtocol2)]){
[_delegate performSelector:#selector(testProtocol2)];
}
}
if(newLocation.x <= 220){
newLocation.x = 220;
//This will trigger the method everytime the spritenode hit's this location
if([_delegate respondsToSelector:#selector(testProtocol)]){
[_delegate performSelector:#selector(testProtocol)];
}
}
self.spriteNode.position = newLocation;
}
}
It will now change the label every time your SpriteNode hits the location you want it to.
I hope this will help other's who are in the same concept as this.
import your appdelegate and then use
ViewController *vc = (ViewController*)[AppDelegate*)[[UIApplication sharedApplication] delegate] viewController];
to parse the object to your presented UIViewController
I have the regular OpenGL / EAGL setup going on:
#interface EAGLView : UIView {
#public
EAGLContext* context;
}
#property (nonatomic, retain) EAGLContext* context;
#end
#implementation EAGLView
#synthesize context;
+ (Class)layerClass {
return [CAEAGLLayer class];
}
#end
#interface EAGLViewController : UIViewController {
#public
EAGLView* glView;
}
#property(nonatomic, retain) EAGLView* glView;
#end
#implementation EAGLViewController
#synthesize glView;
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
for (UITouch* touch in touches) {
CGPoint location = [touch locationInView:glView];
int index;
for (index = 0; index < gCONST_CURSOR_COUNT; ++index) {
if (sCursor[index] == NULL) {
sCursor[index] = touch;
break;
}
}
}
[super touchesBegan:touches withEvent:event];
}
That implementation includes corresponding touchesEnded/Canceled/Moved as well. The code fully works and tracks well.
I also make sure that I'm giving proper values for everything:
sViewController = [EAGLViewController alloc];
CGRect rect = [[UIScreen mainScreen] applicationFrame];
sViewController.glView = [[EAGLView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
Assert(sViewController.glView);
sViewController.glView.userInteractionEnabled = YES;
sViewController.glView.multipleTouchEnabled = YES;
sViewController.glView.exclusiveTouch = YES;
It all compiles just fine, but I'm never receiving more than one UITouch. I don't mean in a single touchesBegan, but the index never goes past 0. I also set a breakpoint for the second time it enters that function, and putting two fingers on doesn't make it trigger.
If you want to detect multiple touches (and/or distinguish between a one finger, two finger etc. touch), try using a UIPanGestureRecognizer. When you set it up, you can specify the minimum and maximum number of touches. Then attach it to the view where you want to detect the touches. When you receive events from it, you can ask it how many touches it received and branch accordingly.
Here's the apple documentation:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPanGestureRecognizer_Class/Reference/Reference.html
If you do this, you might not need to use the touchesBegan/Moved/Ended methods at all and, depending on how you set up the gesturerecognizer, touchesBegan/Moved/Ended may never get called.
Use [event allTouches] in place of touches. touches represents only the touches that have 'changed'. From the apple docs:
If you are interested in touches that have not changed since the last
phase or that are in a different phase than the touches in the
passed-in set, you can find those in the event object. Figure 3-2
depicts an event object that contains touch objects. To get all of
these touch objects, call the allTouches method on the event object.
It seems all I was missing was this:
sViewController.view = sViewController.glView;
I have found the following code and I need help with editing it. I am not really familiar with texture rendering.
First of all, init method takes a rect and magnifies only that area? How can I make it more dynamic and magnify only whatever is underneath the magnifying glass?
Secondly, Is it possible to change the shape to circle rather than rectangle?
Or Can I use an image as the frame of the magnifying glass?
Here is the code..
Cheers..
.h file
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface Magnify : CCNode {
BOOL active;
CGRect rect;
CGFloat magnifyScale;
CCNode *renderNode;
CCRenderTexture *renderTexture;
}
- (id)initWithNodeToMagnify:(CCNode *)n rect:(CGRect)rectToMagnify scale:(CGFloat)scale;
- (void)enable;
- (void)disable;
.m file
#import "Magnify.h"
#implementation Magnify
- (id)initWithNodeToMagnify:(CCNode *)n rect:(CGRect)rectToMagnify scale:(CGFloat)scale
{
if (self = [super init]) {
self.visible = active = NO;
renderNode = n;
rect = rectToMagnify;
magnifyScale = scale;
renderTexture = [[CCRenderTexture renderTextureWithWidth:rect.size.width height:rect.size.height] retain];
[self addChild:renderTexture];
}
return self;
}
- (void)enable
{
self.visible = active = YES;
[self scheduleUpdate];
}
- (void)disable
{
self.visible = active = NO;
[self unscheduleUpdate];
}
- (void)drawAreaToTexture
{
[renderTexture beginWithClear:0.0 g:0.0 b:0.0 a:1.0];
// shift the renderNode's position to capture exactly the rect we need
CGPoint originalPosition = renderNode.position;
renderNode.position = ccpSub(originalPosition, rect.origin);
// scale the node as we want
CGFloat originalScale = renderNode.scale;
renderNode.scale = magnifyScale;
[renderNode visit];
// shift renderNode's position back
renderNode.position = originalPosition;
// scale back
renderNode.scale = originalScale;
[renderTexture end];
}
- (void)update:(ccTime)dt
{
[self drawAreaToTexture];
}
- (void)dealloc
{
[renderTexture release];
[super dealloc];
}
#end
OK, so, as I mentioned above for something like this, one possible answer is to use the CCLens3D class to get the "effect" of magnifying something in a circular manner.
I found using this to be a little tricky because it doesn't seem to work unless it's a child of the top level node of your 'scene'.
Here is some code I use to create a lens that moves around the screen, and then disappears:
// Create the lens object first.
//
CCLens3D *lens =
[CCLens3D actionWithPosition:fromPos
radius:50
grid:ccg(50, 50)
duration:2.0];
// Set the "size" of the lens effect to suit your needs.
//
[lens setLensEffect:1.0];
// In my case, I then move the lens to a new position. To apply an action on
// a lens, you need to give the actions to the actionManager in the
// CCDirector instance.
//
CCMoveTo *move = [CCMoveTo actionWithDuration:2.0 position:toPos];
// I had another action in this array, but this will do.
//
CCSequence *seq = [CCSequence actions:move, nil];
// Now tell the actionManager to move the lens. This is odd, but it works.
//
[[[CCDirector sharedDirector] actionManager] addAction:seq target:lens paused:NO];
// Now just for some more weirdness, to actually make the lens appear and operate
// you run it as an action on the node it would normally be a child of. In my case
// 'self' is the CCLayer object that is the root of the current scene.
//
// Note that the first action is the lens itself, and the second is a special
// one that stops the lens (which is a "grid" object).
//
[self runAction:[CCSequence actions:lens, [CCStopGrid action], nil]];
I imagine that you should be able to stop the grid by running the CCStopGrid action when you want to. In my case it is a programmed thing. In yours it might be when the user lets go of a button.
Situation:
I'm getting some mysterious crashing shortly after a CCCallFunc. In short, we have a button. The button has a tag to identify it later. When the button is pressed, we run some actions to animate it, and when the animation is done, we CCCallFunc another method to transition to another scene. We crash shortly after the CCCallFunc. Source and errors below.
Point Of Crash (in cocos2d source):
// From CCActionInstant.m of cocos2d
-(void) execute
{
/*** EXC_BAD_ACCESS on line 287 of CCActionInstant.m ***/
[targetCallback_ performSelector:selector_];
}
#end
Snapshot of Thread 1:
My Code:
Below is some source taken from MenuLayer.m (a simple menu to display a button).
// from MenuLayer.m
// …
#implementation MenuLayer
-(id) init{
if((self=[super init])) {
/****** Create The Play Button (As a CCMenu) ********/
CCSprite *playSprite = [CCSprite spriteWithFile:#"playbutton.png"];
CCMenuItemSprite *playItem = [CCMenuItemSprite itemFromNormalSprite:playSprite selectedSprite:nil target:self selector:#selector(animateButton:)];
playItem.tag = 3001;
playItem.position = ccp(160.0f, 240.0f);
CCMenu *menu = [CCMenu menuWithItems:playItem, nil];
menu.position = ccp(0.0f, 0.0f);
[self addChild:menu z:0];
}
}
// ...
- (void)animateButton:(id)sender{
/*** Run an animation on the button and then call a function ***/
id a1 = [CCScaleTo actionWithDuration:0.05 scale:1.25];
id a2 = [CCScaleTo actionWithDuration:0.05 scale:1.0];
id aDone = [CCCallFunc actionWithTarget:self selector:#selector(animationDone:)];
[sender runAction:[CCSequence actions:a1,a2,aDone, nil]];
}
- (void)animationDone:(id)sender{
/*** Identify button by tag ***/
/*** Call appropriate method based on tag ***/
if([(CCNode*)sender tag] == 3001){
/*** crashes around here (see CCActionInstant.m) ***/
[self goGame:sender];
}
}
-(void)goGame:(id)sender{
/*** Switch to another scene ***/
CCScene *newScene = [CCScene node];
[newScene addChild:[StageSelectLayer node]];
if ([[CCDirector sharedDirector] runningScene]) {
[[CCDirector sharedDirector] replaceScene:newScene]];
}else {
[[CCDirector sharedDirector] runWithScene:newScene];
}
}
Use CCCallFuncN instead of CCCallFun.
CCCallFuncN passes the Node as parameter, the problem with CCCallFun is that you are loosing reference of the node.
I test your code with CCCallFuncN and works ok.
Just a hunch. Besides checking for memory leaks, try to schedule a selector with a 0 second interval instead of directly sending the goGame message. I have a suspicion that director's replaceScene causes a cleanup of the scene and all objects associated with it. That in turn could leave the CCCallFunc action in an undefined state. Although normally it works fine - which is to say that this is just another indication about something sketchy, memory- respectively object-lifetime-management-wise.
Btw, if you support iOS 4 as a minimum, use CCCallBlock instead of CCCallFunc. That's safer and cleaner.