ccfollow actions in cocos2d - iphone

ccfollow actions is following my projectile correctly. when, double tap recognised.
see the below code. were am mistaken.
here is my code:-
- (void)update:(ccTime)dt {
if (numTaps==1 ) {
[self runAction:[CCFollow actionWithTarget:nextProjectile worldBoundary:CGRectMake(0,0,1050,350)]];
}
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
numTaps = [[touches anyObject] tapCount];
printf("Tapcount : %d",numTaps);
if (numTaps ==1) {
nextProjectile = [[CCSprite spriteWithFile:#"Weapon.png"] retain];
nextProjectile.position = ccp(nextprojectile .contentSize.width/2+65, nextprojectile.contentSize.height/2+70);
[nextProjectile runAction:[CCSequence actions: [here's my nextprojectile actions],nil]];
}
}
after seen the above code. a question raised by you.
why am mentioning the ccfollow action in if condition.
answer is here,
ordinarily, am mention in update function it will not works, when i set this if condition it works after double tap recognised.
how to rectify this issue?
any help would be highly appreciated.

not in update, try this coding in
[self runAction:[CCFollow actionWithTarget:nextProjectile worldBoundary:CGRectMake(0,0,1050,350)]];
}
when adding your projectile. some thing like in your code [self addchild: nextprojectile];

What is the problem/issue - you mention double taps, but then code is "numTaps == 1".
Are you saying it only works for double taps, but you want it to work for single taps?
What is working? What is not working?
Have you tried the cocos2d forums?

Related

how to implement Undo feature in drawing so that user can clear his last line

I am using CoreGraphics to implement free hand drawing which is working fine for me and now I want to implement Undo feature for this drawing so that user can clear his last strok
In your method:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(imageView2.image != nil){
[pathArray addObject:imageView2.image];
}
}
And on undo button:
if([pathArray count]>0)
{
[pathArray removeLastObject];
if([pathArray count]==0)
{
imageView2.image = GlobalImage2;
}
else
{
imageView2.image=[pathArray objectAtIndex:[pathArray count]-1];
}
}
Retrive this image from array.
I have used same code in my few apps. I hope it'll be useful for you.
Thanks,
Hemang.
You should read about the Memento Pattern. Some links:
http://www.oodesign.com/memento-pattern.html
http://java.dzone.com/articles/design-patterns-memento
In time, sorry for not explaining it here. But there are tons of books and articles about it.
I never used this but you can find over here to undo or clear last things
http://www.cocoacontrols.com/controls/acedrawingview?utm_source=Cocoa+Controls&utm_campaign=eaef7635b9-Cocoa_Controls_Weekly_Roundup_2013_01_1%27&utm_medium=email

iphone cocos2d v2.0.0-rc0 - can't disable touch

I've been trying to resolve a problem for a long time, unfortunately i couldn't find a solution. I hope you can help me...
I'm trying to develop a turn based game. Player 1 is the user, player 2 is the iphone. I want to receive touches when it's the player1's turn.
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (self.playerNo==1) { //control player1
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self selectSpriteForTouch:touchLocation];
return YES;
}
//if it's not player1's turn, return NO
return NO;
}
When it's iphones turn, I want to disable touches during 5 seconds. I tried all the ways below, but no success.
-(void)iphonesTurn
{
[NSThread sleepForTimeInterval:5];
//
//game logic...
//
}
During 5 seconds I touch screen several times. I realised that all the touches looks like stored and the app reacts to all of my touches when it's the player1's turn.
How can I completely disable touches?
Thanks for your help.
I don't know where is located your snippet of code but if it runs into the main thread you must NEVER block (pause) it!
Try to do something like that (it works with the final version of cocos2d 2.0):
- (void)iphonesTurn
{
[[[CCDirector sharedDirector] touchDispatcher] setDispatchEvents:NO];
[self scheduleOnce:#selector(enableTouch) delay:5.0f];
}
- (void)enableTouch
{
[[[CCDirector sharedDirector] touchDispatcher] setDispatchEvents:YES];
}

(cocos2d sneaky input) when setting joystick position with touchLocation, sprite won't move

i want to let user choose where the joystick should be. i.e., when user touch at one location, the joystick will appear there and ready to use and will remove when finger released.
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([self getChildByTag:kTagJoyStick] == nil) {
[self addJoystickWithPosition:[Helper locationFromTouches:touches]];
}
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([self getChildByTag:kTagJoyStick] != nil) {
[self removeChildByTag:kTagJoyStick cleanup:YES];
}
}
-(void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self ccTouchesEnded:touches withEvent:event];
}
(do nothing in ccTouchesMoved method)
the update methods for joystick is:
-(void) sneakyUpdate {
if ([self getChildByTag:kTagJoyStick] != nil) {
if (joystick.velocity.x < 0) {
[self controlLeft];
}
else if (joystick.velocity.x > 0) {
[self controlRight];
}
else {
[self controlStop];
}
}
else {
[self controlStop];
}
}
but the result is, the joystick will appear and auto remove. but my sprite won't move. ( i set the break point, the sneakyUpdate method did get called. but the joystick.velocity is always 0. (and the thumbSprite didn't follow our finger.
please help me.
update:
and it turns out that i have to use 2 fingers (one for touch once and let the joystick show up, move my finger away, and then use another finger to control the joystick)
I'm not 100% sure, but I think you should use ccTouchBegan instead ccTouchesBegan, because sneakyJoystick classes use ccTouchBegan/Moved/Ended/Cancelled. Also, there are for a single touch, that is what you want.
I hope it works!
It looks like the problem is in your joystick class. Every joystick implementation I've seen uses the ccTouchesBegan method to activate the joystick, then in the ccTouchesMoved method, it makes sure its activated before using it. The problem I am seeing is that you create and add the joystick AFTER the touches began method, meaning your joystick never 'activates'. One way of bypassing this is to do all of the joystick's ccTouchesBegan functions in the method that creates the joystick, and 'activate' it from there by passing a reference to the touch that will be using it.

UIImageView scope. Accessing from another class

Here's part of the code I'm working with: http://pastie.org/2472364
I've figured out how to access the UIImageView from another method within the same class file in which it was programmatically created.
However, I was wondering how I'd access that same UIImageView from within the LetterTiles.m file, specifically within the touchesMoved method. The way I wrote the code in the sample, it will only show if the frames intersect if they're on top of each other when the otherMethod is called. Of course, I need to be able to check if the views intersect within the actual touchesMoved method. I'm sure it's something super easy, but I'm just not sure how to do it.
Thanks in advance for any help you can give me.
From your comment, and using the code you already have, I would go down this route. This isn't what I would do personally, just FYI. The structure is a bit shakey with the way it sounds like you want this.
Create the place holder UIImageView in the touchesBegan function, then check to see if they intersect when the user stops moving the image.
#import "LetterTiles.h"
#implementation LetterTiles
#synthesize placeHolder;
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
// Retrieve the touch point (I consider this useful info to have, so I left it in)
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;
// Create a place holder image wherever you want
[self setPlaceHolder:[[[UIImageView alloc] initWithFrame:CGRectMake(39, 104, 70, 70)] autorelease]];
[newImage setImage[UIImage imageNamed:#"placeHolder.png"]] autorelease];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:[self superview]];
[self setCenterPoint:pt];
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
LetterTiles *movingTile = self;
if (CGRectIntersectsRect([movingTile frame], [placeHolder frame])) {
NSLog(#"Touched");
[self setFrame:[placeHolder frame]];
}
}
Make a protocol called ViewMoved which will contain one method otherMethod.
implement that in myMainViewController
take a delegate property of type ViewMoved in LetterTiles.
Assign self when you make new object of type LetterTiles in myMainViewController.
On every movement of touch call oherMethod of delegate and check whether any views of type LetterTiles are intersecting or not.
This will catch any intersection when any of the view is moved.....
If above is not matching with your question then write here......

Implementing iphone's copy/paste controls on a custom view / uiview subclass

I will admit that there is already a question exactly along these lines here on S.O., but it lacks implementation details, a working answer, and I would like to be more specific, so I think a new question is in order. Obviously, let me know if I'm wrong and we can try to restart the thread over there.
Basically, I want to copy the text in a UILabel to the pasteboard when a user holds down on the label. Not hard to do, honestly. However, I think the best way to provide visual feedback is to prompt the user with the Copy menu option from UIMenuController.
According to the Event Handling section of the iPhone Application Programming Guide, specifically the section on Copy, Cut, and Paste Operations, it should be possible to provide copy, cut, and/or paste operations from a custom view.
So, I've sub-classed UILabel with the following implementation as described by the guide, but the UIMenuController won't show up. There's no indication in the guide that there's anything else required to do this, and the NSLog statement does print out to the console, indicating that the selector is being performed when I hold down on the label:
//
// CopyLabel.m
// HoldEm
//
// Created by Billy Gray on 1/20/10.
// Copyright 2010 Zetetic LLC. All rights reserved.
//
#import "CopyLabel.h"
#implementation CopyLabel
- (void)showCopyMenu {
NSLog(#"I'm tryin' Ringo, I'm tryin' reeeeal hard.");
// bring up editing menu.
UIMenuController *theMenu = [UIMenuController sharedMenuController];
// do i even need to show a selection? There's really no point for my implementation...
// doing it any way to see if it helps the "not showing up" problem...
CGRect selectionRect = [self frame];
[theMenu setTargetRect:selectionRect inView:self];
[theMenu setMenuVisible:YES animated:YES]; // <-- doesn't show up...
}
// obviously, important to provide this, but whether it's here or not doesn't seem
// to change the fact that the UIMenuController view is not showing up
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
BOOL answer = NO;
if (action == #selector(copy:))
answer = YES;
return answer;
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self performSelector:#selector(showCopyMenu) withObject:nil afterDelay:0.8f];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(showCopyMenu) object:nil];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(showCopyMenu) object:nil];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(showCopyMenu) object:nil];
}
#end
So, what else does one have to do to make this happen?
For those following along and trying to do this, too, you'll also need to set 'User Interaction Enabled' for the label
Edit:
For clarity, let me add that this should be similar to the little [Copy] menu item that appears over an image in certain iphone views when you hold down on it. -B
I'll say upfront I don't have an aswer, but I did some poking around and found more out. I'm sure you've looked at this already: CopyPasteTile
That code does work on my simulator and goes like this:
CGRect drawRect = [self rectFromOrigin:currentSelection inset:TILE_INSET];
[self setNeedsDisplayInRect:drawRect];
UIMenuController *theMenu = [UIMenuController sharedMenuController];
[theMenu setTargetRect:drawRect inView:self];
[theMenu setMenuVisible:YES animated:YES];
There are a few differences here:
drawRect is calculated from a giant view tile and tap point calculations
setNeedsDisplayInRect is being called
self is a large screen sized view, you may need screen coords instead of local coords (you can probably get that from self.superview)
I'd try making these adjustments to match the example first and see what kind of progress it gets me.