English 中文(简体)
Dragging a particularly large sprite up and down like scroll effect in COCOS2D
原标题:

I am really stuck on this. My application is in landscape view and on one screen i wanted my instructions image to be scrollable. I have added this image as a sprite. First i tried to get scroll effect available from other sites, but soon i saw that scrolling was being done for the complete screen and not the sprite image. Then i resorted to implement the scrolling effect by dragging the sprite only in y axis (up and down). Unfortunately i am messing things somewhere, due to which only a portion of the sprite (shown on the screen only with the height 320pixels) is being dragged and the rest of the sprite is not being shown. The code is as follows

in the init layer function i have

//Add the instructions image

 instructionsImage = [Sprite spriteWithFile:@"bkg_InstructionScroll.png"];
 instructionsImage.anchorPoint = CGPointZero;
 [self addChild:instructionsImage z:5];
 instructionsImage.position = ccp(40,-580);
 oldPoint = CGPointMake(0,0);
 self.isTouchEnabled = YES;

//The touch functions are as follows
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = [touches anyObject];

 // Move me!
 if(touch && instructionsImage != nil) {
  CGPoint location = [touch locationInView: [touch view]];
  CGPoint convertedPoint = [[Director sharedDirector] convertCoordinate:location];

  CGPoint newPoint = CGPointMake(40, (instructionsImage.anchorPoint.y+ (convertedPoint.y - oldPoint.y)));
  instructionsImage.position = newPoint;
  return kEventHandled;
 }
 return kEventIgnored;
}

//The other function
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = [touches anyObject];

 // Move me!
 if(touch && instructionsImage != nil) {
  CGPoint location = [touch locationInView: [touch view]];
  CGPoint convertedPoint = [[Director sharedDirector] convertCoordinate:location];
  oldPoint = convertedPoint;
  return kEventHandled;
 }

 return kEventIgnored;
}
问题回答

Your approach is generally correct.

The code did not format properly, and you were not clear on exactly what the symptom is of the problem...

But it appears as if your math in the ccTouchesMoved is wrong. The anchor point is not what you care about there, as that s just a ratio within the image where the position and rotation anchor occurs. Set that, like you do, to whatever makes sense in the constructor but after that you don t need to reference it.

Try just adding your movement to the sprite:

deltaY = convertedPoint.y - oldPoint.y;

Now you know how many pixels your finger has moved up and down.

Reset your oldPoint data for next time:

oldPoint.y = convertedPoint.y;

Now apply this delta to your sprite:

instrucitonsImage.position = ccp(instructionsImage.position.y,instructionsImage.position.y + delta);

Should do it.





相关问题
how to detect Android ListView Scrolling stopped?

I m trying to do something after scrolling stopped.So, I tried using OnScrollListener#onScrollStateChanged(SCROLL_STATE_IDLE) to detect when the scrolling stopped(either TOUCH_SCROLL or FLING)(at 1.5 ...

jQuery - ScrollTo and Serial Scroll not working together

I have tested the scrollTo() plugin, but I need a way to stop the scrolling animation, so I am looking at serialScroll(). Here is what I used with scrollTo: $( #scroller ).scrollTo( 1000px , 3000); ...

Scrolling to the bottom of a div

As part of an ajax chat room (using prototype framework) I am working on I scroll the user to the bottom each time a message is added using the below js. However if the user has scrolled up and the ...

Displaying news with javascript

I keep coming up against this issue. Displaying news on a website. And I d like to come up with a solution to use like a template, something I can just drop on a site easily. The solution I keep ...

UITextview content moves up when textview is selected

I am pretty new to this iPhone dev thing but so far I have built a pretty good app but I have stumbled into this problem that for the life of me I cannot seem to solve. Basically the app is a chat ...

热门标签