sprite detection problem - iphone

I have two pair of sprites on the screen.when two sprites are clicked i want to check if the clicked two sprites are same or not,if they are same then remove from screen.can anyone please give me any suggestions to do this.
thanks.
this is the code i have done so far..
NSString *name = [NSString stringWithFormat:#"gimg.png"];
CCTexture2D * texture = [[CCTextureCache sharedTextureCache] addImage:name];
NSMutableArray *imgFrameTemp = [NSMutableArray array];
for (int i = 0; i <2; i++) {
CCSpriteFrame *imgFrame1 = [CCSpriteFrame frameWithTexture:texture rect:CGRectMake(i*50, 0*50, 50, 50)];
CCSpriteFrame *imgFrame2 = [CCSpriteFrame frameWithTexture:texture rect:CGRectMake(i*50, 0*50, 50, 50)];
[imgFrameTemp addObject:imgFrame1];
[imgFrameTemp addObject:imgFrame2];
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
int ran = arc4random()%[imgFrameTemp count];
CCSpriteFrame * img = [imgFrameTemp objectAtIndex:ran];
CCSprite *sprite = [CCSprite spriteWithSpriteFrame:img];
sprite.anchorPoint = ccp(0,0);
sprite.position = ccp(i*60,(j+1)*60);
[self addChild:sprite];
[imgFrameTemp removeObjectAtIndex:ran];
}
}
Now my Four Sprites are on the screen, i want to check if two same sprites are clicked and remove them.

do u want to check two sprite image is same?
if it is correct "CCSprite does not provide image name so u set manually ".CCSprite have property named "userData" .
//get touch sprites user Data
NSString *str=spr.userData;
Nsstring *str1=spr1.userData;
if ([srt isEqualToString:str1)
{
CCLOG(#"two sprites are same");
}

Related

myParticleEmitter weirdness (creates artifact at 0.0) while drawing from point A to point B

A little weirdness here, I have a particle emitter added to an SKShapeNode
(the particles should follow the line)
yet when it does it also has a bit of particle system sitting at 0,0
Ive moved it up a little so you can see it better
Any thoughts to how this might be happening?,
I'll post the code below, but still I cant find anything, I have tested with other textures setting setStrokeTexture - incase the line is making some sort of sneaky extra part - but no it seems fine....and creates a line between the sprites...
in LinkWithNumberClass //SKSpriteNode SKPhysicsContactDelegate
if (isUsingParticles == YES) {
//create new line
SKShapeNode*lineNode02 = [SKShapeNode node];
lineNode02.lineWidth = 10;
lineNode02.name = #"lineNode";
//clear line of colour
lineNode02.fillColor = [SKColor clearColor];
lineNode02.strokeColor = [SKColor clearColor];
//create the path the line follows (line joins two sprites)
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, spriteA.position.x, spriteA.position.y);
CGPathAddLineToPoint(pathToDraw, NULL, spriteB.position.x, spriteB.position.y);
lineNode02.path = pathToDraw;
[self addChild:lineNode02];
//Add Particles
myParticlePath = [[NSBundle mainBundle] pathForResource:_parName ofType:#"sks"];
myParticleEmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
[self addChild:myParticleEmitter];
CGFloat distancebtween = SDistanceBetweenPoints(spriteA.position, spriteB.position);
myParticleEmitter.particlePositionRange = CGVectorMake( particleLength,0 );
myParticleEmitter.position = CGPointMake(distancebtween/2, +10);
self->myParticleEmitter.targetNode = self.scene;
int particleTime = 3;
SKAction *followTrack =
[SKAction followPath:pathToDraw
asOffset:YES
orientToPath:YES
duration:particleTime];
SKAction *forever = [SKAction repeatActionForever:followTrack];
self->myParticleEmitter.particleAction = forever;
}
In Game Layer
-(void)addStaticLinkedSpriteWithParticles
{
twoSpritesWithParticlesBridge =
[[LinkWithNumber alloc]initWithlinkSpriteA:#"Object"
spriteB:#"Object"
andPlistAnimation:#"Aniamtions"
distbetween:100
hasParticles:YES
ParticlesNamed:#"Fire"];
[self addChild:self->twoSpritesWithParticlesBridge];
}
With thanks :)
Natalie.
In the end I go it to work not needing to draw a line at all.
Create particle system Position on SpriteA
find distance between
the two sprites (to get length for particles)
Adjust length of the particles in emitter properties
reposition the
particles so they are centered between the two sprites
if (isUsingParticles == YES) {
//create particles
myParticlePath = [[NSBundle mainBundle] pathForResource:_parName ofType:#"sks"];
myParticleEmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
[self addChild:myParticleEmitter];//try moving it to the line node
//find length needed for particles
CGFloat distancebtween = SDistanceBetweenPoints(spriteA.position, spriteB.position);
//Adjust length of particles
myParticleEmitter.particlePositionRange = CGVectorMake( particleLength,0 );
//center particles between the two sprites
myParticleEmitter.position = CGPointMake(spriteA.position.x+distancebetween/2, spriteA.position.y);
myParticleEmitter.targetNode = self.scene;
}
Hope it helps someone else :)

How to Dynamically Load Images Using UIScrollview

I am bit confused how to accomplish to load images dynamically using UIScrollview or other objects ...
![Dynamically displaying images coming from webservice][1]
In the above image it supports both horizontal and vertical scrolling of images. Can we do this using UIScrollview or other objects like UICollectionView?
Below is the general code for horizontal scrollview for displaying 7 images,
int x=10;
int y=20;
for (int i=1; i<=7; i++)
{
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 80, 80)];
bgView.tag=i;
UIImageView *bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 55, 55)];
bgImgView.tag=1;
bgImgView.image=[UIImage imageNamed:[NSString stringWithFormat:#"img_%d.png",i]];
[bgView addSubview:bgImgView];
[self.videoscrollview addSubview:bgView];
x=x+80;
}
self.scrollview.contentSize=CGSizeMake(7*80, 84);
Can we handle with one UIView and UIImageView like above method using UIScrollview for displaying images?
I want to handle UIViews with different sizes which are subviews to UIScrollview...
I googled but not found any related stuff. Any suggestions to deal with this?
Assuming that your image names are Likes: images_1/2.../10.../n.(Make sure daynamic image coming from this imagename)
Try this one :
int x=10; int y=20;
for (int i=1; i<=7; i++)
{
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 80, 80)];
bgView.tag=i;
UIImageView *bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 55, 55)];
bgImgView.tag=1;
NSString *image = [NSString stringWithFormat:#"images_%d.png",i];
bgImgView.image = [UIImage imageNamed:image];
[bgView addSubview:bgImgView];
[self.videoscrollview addSubview:bgView];
x=x+80;
}
self.scrollview.contentSize=CGSizeMake(7*80, 84);
check that it's working or not,may be it will work?
happy coding.
For this, first you have to calculate and check for the visible area of scrollview i.e. contentOffset , then you have to load objects for that visible part and as you alloc new object you should store them in an array. As any image moves outside the visible area you should remove it from scrollview.
This way it will take less memory and you could load many images without getting memory warning.
You could follow this article to get some hint how to add and remove objects from scrollView
http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
Try this for dynamic image From NSBundle:
NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resourcePath error:&error];
NSMutableArray *imageArray = [[NSMutableArray alloc]init];
for (int i=0; i<directoryContents.count; i++) {
NSString *pathExtension = [[directoryContents objectAtIndex:i] pathExtension];
if ([pathExtension isEqualToString:#"png"] || [pathExtension isEqualToString:#"jpg"] ) {
[imageArray addObject:[directoryContents objectAtIndex:i]];
}
}
Now u have array of all images
for (int i=1; i<=imageArray.count; i++)
{
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 80, 80)];
bgView.tag=i;
UIImageView *bgImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 55, 55)];
bgImgView.tag=1;
bgImgView.image=[UIImage imageNamed:[imageArray objectAtIndex:i]];
[bgView addSubview:bgImgView];
[self.videoscrollview addSubview:bgView];
x=x+80;
}
self.scrollview.contentSize=CGSizeMake(i*80, 84);
Thanks for your valuable answers. I fixed temporarily above issue using below code.
Bool isOddViewNo; //at .h file
isOddViewNo=YES; //at viewDidLoad
int x=10;
int y=10;
for (int i=0; i<=15; i++)
{
if (isOddViewNo)
{
isOddViewNo=NO;
UIView *oddView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 90, 90)];
UIImageView *oddImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 85, 85)];
oddImgView.image=[UIImage imageNamed:[NSString stringWithFormat:#"img_%d.png",i]];
[oddView addSubview:oddImgView];
[self.scrollview addSubview:oddView];
x+=90;
}
else
{
isOddViewNo=YES;
UIView *evenView=[[UIView alloc]initWithFrame:CGRectMake(x, y, 150, 150)];
UIImageView *evenImgView=[[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 145, 145)];
evenImgView.image=[UIImage imageNamed:[NSString stringWithFormat:#"img_%d.png",i]];
[evenView addSubview:evenImgView];
[self.scrollview addSubview:evenView];
x+=150;
}
if ((i+1)%3==0)
{
//if added image is 4 th image
y+=150;
x=10;
}
if (y+150>self.scrollview.frame.size.height)
{
self.scrollview.contentSize=CGSizeMake(400, y+150);
}
else
{
self.scrollview.contentSize=CGSizeMake(320, self.scrollview.frame.size.height+150);
}
}
![I am Displaying three views with different sizes in each row of UIScrollview. Which is scrollable both horizontally and Vertically. Here is the screen shot taken from simulator][1]

How to put animation between frames spritesheet in cocos2d?

I am new to cocos2d .Can any one suggest me a method to give animation for the transition between one frame to another frame.
this is my code to fetch frames and just display
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"spritesheet.plist"];
// Create a sprite sheet with the Happy Bear images
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"spritesheet.png"];
[self addChild:spriteSheet];
// Load up the frames of our animation
walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <=7; ++i) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"sprite%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
// Create a sprite for our bear
CGSize winSize = [CCDirector sharedDirector].winSize;
self.sprite = [CCSprite spriteWithSpriteFrameName:#"sprite.png"];
self.sprite.position=ccp(320,480);
self.sprite.anchorPoint=ccp(1,1);
self.walkAction = [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO];
How to do the animation for the transition between frames
thanks in advance
You can not "transition" (morph) between two frames (images), if that's what you mean.
It sounds to me like you want something that's called keyframe interpolation, which is normally used in 3D animations. A 2D animation displays one frame after another. You can not interpolate between two frames graphically (morphing), you can only interpolate properties like position.

Please help! Creating new objects (from array?) after an object completes task

I am writing an iPhone application and here is the overall synopsis:
An object is on the screen and moves based on accelerometer input - I have that working - it moves to the edge of the screen and doesn't go off = perfect. Now, what I want to happen is as soon as that object hits any of the four screen edges, it should stop and stay put on the screen, and a new object should 'appear' and start moving due to the accelerometer input, so now two objects would be on the screen, but only 1 moving. Eventually there could be 20 objects built up around the edge, but only 1 object will be moving at a time.
So I have now gotten the help I needed to check for edge hits etc, but I am now trying to switch the way I was getting boxes to show up on the screen. I originally was putting images on the screen through the view controller, but now what I want to do is start with one box in the center, when it hits an edge, it should stop and stay, and a new image will appear in the center and start moving due to accel input as described above. So do I just use an array to pull the images from? Do I not even put .png's on the view controller and should I just code it? Here is some of what I have trying to do this through an array:
//In my .h
UIImageView *blocks;
NSString *blockTypes[3];
//In my .m
blockTypes[0] = #"greenBox1.png";
blockTypes[1] = #"greenBox2.png";
blockTypes[2] = #"greenBox3.png";
Thanks in advance for any help! The help so far has been great!
You should't test if newX and newY are equal to 30 and 50. You should test if they are less than 30 and 50 respectively.
Edit:
I would do it like this:
loat newX = [mutableBoxArray lastObject].center.x + (accel.x * 12);
float newY = [mutableBoxArray lastObject].center.y + (accel.y * -12);
if(newX > 30 && newY > 50 && newX < 290 && newY < 430) {
[[mutableBoxArray lastObject] setCenter: CGPointMake(newX, newY)];
} else {
MyBox *myBox = [[MyBox alloc] init];
[mutableBoxArray addObject: myBox];
[myBox release];
}
Edit 2:
Add the following in your interface file
NSMutableArray *mutableBoxArray;
NSArray *imageNamesArray;
Then in your implementation file in the loadView add
mutableBoxArray = [[NSMutableArray alloc] init];
imageNamesArray = [[NSArray alloc] initWithObjects: #"orangeBox1.png",
#"blueBox1.png", #"greenBox1.png", #"pinkBox1.png", nil];
Then change the above method to
static NSInteger imageInt = 0;
loat newX = [mutableBoxArray lastObject].center.x + (accel.x * 12);
float newY = [mutableBoxArray lastObject].center.y + (accel.y * -12);
if(newX > 30 && newY > 50 && newX < 290 && newY < 430) {
[[mutableBoxArray lastObject] setCenter: CGPointMake(newX, newY)];
} else {
if (imageInt < [imageNamesArray count]) {
UIImage *image = [UIImage imageNamed: [imageNamesArray objectAtIndex: imageInt++]];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setCenter: CGPointMake(100.0f, 100.0f)];
[mutableBoxArray addObject: imageView];
[imageView release];
}
}

cocos2d sprite collision detection boundingbox

I got 2 sprites. I use the boundingbox to check for collision with CGRectIntersectsRect. But it is not working.
HBBall and HBpaddle has a CCSprite called image.
Init:
ball = [[HBBall alloc] init];
ball.position = ccp(150, 50);
[self addChild:ball];
[update addObject:ball];
paddle1 = [[HBPaddle alloc] init];
paddle1.position = ccp(50, 160);
[self addChild:paddle1];
Update:
if (CGRectIntersectsRect([paddle1.image boundingBox], [ball.image boundingBox]))
CCLOG(#"ball hit paddle");
CGRectIntersectsRect retuns always true. Does anyone have an idea?
you cant pass directly the bounding box, because it's relative to the sprite. You MUST pass the absolute CGRect boundingbox like this:
s = CCsprite
s.anchorPoint = ccp(0, 0);
CGRect absoluteBox = CGRectMake(s.position.x, s.position.y, [s boundingBox].size.width, [s boundingBox].size.height);
make necessary adjustments!
hope can help!
http://www.iphonedevsdk.com/forum/iphone-sdk-game-development/17082-cocos2d-collision-detection-between-sprites.html ? Have you googled? This seems like it would be a pretty basic issue in the cocos2d framework.