hello everybody,
i use ScrollView, can not start a menu callback function..
CCMenuItemSprite *item = [CCMenuItemSprite itemFromNormalSprite:spritebk selectedSprite:nil target:self selector:@selector(callback1:)];
CCMenu* pmen = [CCMenu menuWithItems:item, nil];
[pmen setPosition:ccp(0,0)];
[scrollLayer addChild:pmen];
whatever i touch the sprite into menu, can not start callback1 function, what's wrong with me ? can anybody help me?
thanks.
here is my code:
//
// ScrollView.h
// tesg2
//
// Created by ricky stone on 12/31/11.
// Copyright (c) 2011 muhe. All rights reserved.
//
#import "CCLayer.h"
#import "cocos2d.h"
typedef enum
{
BounceDirectionGoingUp = 1,
BounceDirectionStayingStill = 0,
BounceDirectionGoingDown = -1
} BounceDirection;
@interface ScrollView : CCLayerColor
{
BounceDirection direction;
CCLayer *scrollLayer;
BOOL isDragging;
float lasty;
float yvel;
int contentHeight;
}
@end
//
// ScrollView.m
// tesg2
//
// Created by ricky stone on 12/31/11.
// Copyright (c) 2011 muhe. All rights reserved.
//
#import "ScrollView.h"
#define FRAME_RATE 60
#define BOUNCE_TIME 0.2f
@implementation ScrollView
-(id) init
{
int width = 400;
int height = 300;
self = [super initWithColor:ccc4(0, 0, 0, 0) width:width height:height];
if(self)
{
isTouchEnabled_ = YES;
isDragging = NO;
lasty = 0.0f;
yvel = 0.0f;
contentHeight = 300;
direction = BounceDirectionStayingStill;
// Setup the scrolling table
scrollLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 0) width:width-10 height:contentHeight];
[scrollLayer setContentSize:CGSizeMake(width-10, contentHeight)];
[scrollLayer setAnchorPoint:ccp(0, 1)];
[scrollLayer setIsRelativeAnchorPoint:YES];
[scrollLayer setPosition:ccp(5, height)];
[self addChild:scrollLayer];
// TEST items!
for(int i = 0; i < contentHeight; i += 70)
{
CCSprite* spritebk = [CCSprite spriteWithFile:@"setting.png"];
[spritebk setPosition:ccp([spritebk contentSize].width/2, i + ([spritebk contentSize].height))];
CCSprite* sprite0 = [CCSprite spriteWithFile:@"Icon.png"];
CCSprite* sprite1 = [CCSprite spriteWithFile:@"Icon.png"];
[spritebk addChild:sprite0];
[spritebk addChild:sprite1];
[sprite0 setPosition:ccp(sprite0.contentSize.width, spritebk.contentSize.height/2)];
[sprite1 setPosition:ccp(spritebk.contentSize.width - sprite1.contentSize.width, spritebk.contentSize.height/2)];
CCMenuItemSprite *item = [CCMenuItemSprite itemFromNormalSprite:spritebk selectedSprite:nil target:self selector:@selector(callback1:)];
CCMenu* pmen = [CCMenu menuWithItems:item, nil];
[pmen setPosition:ccp(0,0)];
[scrollLayer addChild:pmen];
}
[self schedule:@selector(moveTick:) interval:1.0f/FRAME_RATE];
}
return self;
}
-(void) callback1: (id) sender
{
NSLog(@"callback1\n");
}
#pragma mark Scheduled Methods
- (void) moveTick: (ccTime)dt
{
CGPoint pos = [scrollLayer position];
if(!isDragging)
{
static float friction = 0.96f;
// positions for scrollLayer
float bottomY = [scrollLayer position].y + [self boundingBox].origin.y - [scrollLayer contentSize].height;
float topY = [scrollLayer position].y + [self boundingBox].origin.y;
// Bounding area of scrollview
float minY = [self boundingBox].origin.y;
float maxY = [self boundingBox].origin.y + [self boundingBox].size.height;
//NSLog(@"Top y: %f, bottom Y: %f", topY, bottomY);
//NSLog(@"\tminY: %f, maxY: %f", minY, maxY);
if(bottomY > minY && direction != BounceDirectionGoingDown)
{
//NSLog(@"Go DOWN! %f > %f", bottomY, minY);
yvel = 0;
direction = BounceDirectionGoingDown;
}
else if(topY < maxY && direction != BounceDirectionGoingUp)
{
//NSLog(@"Go UP! %f < %f", topY, maxY);
yvel = 0;
direction = BounceDirectionGoingUp;
}
if(direction == BounceDirectionGoingUp)
{
//NSLog(@"Going up!");
if(yvel <= 0)
{
float delta = (maxY - topY);
float yDeltaPerFrame = (delta / (BOUNCE_TIME * FRAME_RATE));
yvel = yDeltaPerFrame;
}
if(round(topY) == maxY)
{
//NSLog(@"Last stop.");
pos.y = round(topY - [self boundingBox].origin.y);
yvel = 0;
direction = BounceDirectionStayingStill;
}
}
else if(direction == BounceDirectionGoingDown)
{
//NSLog(@"Going Down!");
if(yvel >= 0)
{
float delta = (minY - bottomY);
float yDeltaPerFrame = (delta / (BOUNCE_TIME * FRAME_RATE));
yvel = yDeltaPerFrame;
}
if(round(bottomY) == minY)
{
//NSLog(@"Last stop.");
pos.y = round(bottomY - [self boundingBox].origin.y + [scrollLayer contentSize].height);
yvel = 0;
direction = BounceDirectionStayingStill;
}
}
else
{
yvel *= friction;
}
pos.y += yvel;
[scrollLayer setPosition:pos];
}
else
{
yvel = (pos.y - lasty)/2;
lasty = pos.y;
}
}
#pragma mark -
#pragma mark Touch Methods
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:YES];
}
/*
- (void) ccTouchesBegan: (NSSet *)touches withEvent: (UIEvent *)event
{
isDragging = YES;
}
- (void) ccTouchesMoved: (NSSet *)touches withEvent: (UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint a = [[CCDirector sharedDirector] convertToGL:[touch previousLocationInView:touch.view]];
CGPoint b = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]];
CGPoint nowPosition = [scrollLayer position];
nowPosition.y += ( b.y - a.y );
[scrollLayer setPosition:nowPosition];
}
- (void) ccTouchesEnded: (NSSet *)touches withEvent: (UIEvent *)event
{
isDragging = NO;
}*/
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
isDragging = YES;
return TRUE;
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint a = [[CCDirector sharedDirector] convertToGL:[touch previousLocationInView:touch.view]];
CGPoint b = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]];
CGPoint nowPosition = [scrollLayer position];
nowPosition.y += ( b.y - a.y );
[scrollLayer setPosition:nowPosition];
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
isDragging = NO;
}
-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
}
- (void) dealloc
{
[super dealloc];
}
@end