cocos2d: using CCEaseBounce to make a simple scroll view control

This is for vertical scroll, but it is easy to change to horizontal.

To use this, just create a CCLayer object (such as CCLayer *l1…, then instantiate with ScrollLayer *scroller = [[ScrollLayer alloc] initWithLayers:[NSMutableArray arrayWithObjects: l1,l2,l3,nil]];

ScrollLayer.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface ScrollLayer : CCLayer {

	// Holds the current height and width of the screen
	int scrollHeight;
	int scrollWidth;
	int startHeight;
	int startWidth;
	int currentScreen;
	int totalScreens;
	int startPos;	
}

-(id) initWithLayers:(NSMutableArray *)layers;

@end

ScrollLayer.m

#import "ScrollLayer.h"

@implementation ScrollLayer

-(id) initWithLayers:(NSMutableArray *)layers
{

	if ( (self = [super init]) )
	{
		[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

		currentScreen = 1;

		scrollWidth = [[CCDirector sharedDirector] winSize].width;
		scrollHeight = [[CCDirector sharedDirector] winSize].height;
		startWidth = scrollWidth;
		startHeight = scrollHeight;

		int i = 0;
		for (CCLayer *l in layers)
		{			
			l.anchorPoint = ccp(0,0);

			l.position = ccp(0, -(i*scrollHeight));
			[self addChild:l];
			i++;
		}

		totalScreens = i;		
	}
	return self;
}

-(void) move
{
	float dest = ((currentScreen-1)*scrollHeight);
	id changePage = [CCEaseBounce actionWithAction:[CCMoveTo actionWithDuration:0.2 position:ccp(0,dest)]];
	[self runAction:changePage];
}

- (void)onExit
{
	[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
	[super onExit];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
	CGPoint touchPoint = [touch locationInView:[touch view]];
	touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

	startPos = touchPoint.y;
	return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{

	CGPoint touchPoint = [touch locationInView:[touch view]];
	touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

	self.position = ccp(0, ((currentScreen-1)*scrollHeight)+(touchPoint.y-startPos));
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
	CGPoint touchPoint = [touch locationInView:[touch view]];
	touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

	int newY = touchPoint.y;

	if ( (newY - startPos) > 50 && (currentScreen+1) <= totalScreens )
		currentScreen++;
	else if ( (newY - startPos) < -50 && (currentScreen-1) > 0 )
		currentScreen--;

	[self move];
}

- (void) dealloc
{
	[super dealloc];
}

@end
Advertisements

box2d: following a moving target body

CGFloat speed = 0.6;
b2Vec2 velocity = target.body->GetLinearVelocity();
b2Vec2 p1 = target.body->GetPosition();
float y = p1.y * b2dPTMRatio;

if (sqrtf(powf(velocity.x, 2) + powf(velocity.y, 2)) < 1)

        b2Vec2 p2 = body_->GetPosition();
        CGFloat x = p1.x - p2.x;
	CGFloat y = p1.y - p2.y;

	speed /= 3;
	if (x != 0) {
		CGFloat oldX = x;
		x = x/fabsf(x) * speed;
		y = x/oldX * y;
	}
	else if(y != 0) {
		CGFloat oldY = y;
		y = y/fabsf(y) * speed;
		x = y/oldY * x;
	}

	body_->SetLinearVelocity(b2Vec2(x,y));
	return;
}

Simple Pacman Demo Source Code

This pacman game/demo with full source code contains the following features:

– uses only the standard SDK
– uses the accelerometer for player movement
– basic enemy AI
– simple level format
– supports multiple levels
– reset the game

Download the Source code