-12

I met two interesting issues today.Below is what i did in my coding

1.In .H file,I declared a float variable "currentPer" and a CCProgressTime variable "_strengthBar"

2.In UIPanGestureRecognizer's call back function "processPanGesture()",I calculated finger's moving distance,Then updated CCProgressTime's percentage attribute.But in screen,The CCProgressTime's visible part is not updated,So my first question is why CCProgressTime is not updated on screen while the percentage attribute is changed?

3.Finally i updated variable "currentPer" with finger's moving distance in call back function processPanGesture() .Then in "update" method,I print the currentPer,But the result is always "0",(If print _stregnthBar's percentage,It is also "0" even though i have changed its value in processPanGesture)Why?I mean i have changed currentPer's value in processPanGesture().But why it doesn't work? If you are still not clear about what i said ,Please see the screen shot.Any help is very precious for me,Thanks for you attention!

Picture 1.AppDelegate.appFinishDidLaunchOption(),Regist PanGestureRecognizer

Picture 2.GameLayer.H file

Picture 3.GameLayer.mm ,init(),Initialize related variables

Picture 4.GameLayer.processPanGesture(),Gesture recognizer's call back function

Picture 5.GameLayer.update()

Regist gesture stuff in AppDelegate Declare float variable "currentPer" and CCProgressTime variable "_stregnthBar"

Initialize  CCProgressTime variable "_stregnthBar" in GameLayer.init()

UIPanGestureRecognizer callback function in GameLayer

GameLayer's update(),print "currentCur",But it is always "0"

Chailie
  • 451
  • 2
  • 11
  • 24
  • 7
    You forgot to include the link and a license to a decent [OCR](http://en.wikipedia.org/wiki/Optical_character_recognition) program with your post. – Joe Nov 13 '12 at 14:53
  • @Joe lol . And Chailie - It would help tremendously if you structured your post in such a way it's easy to follow the variable you're asking about, you're posting quite a lot of code. – Jens Bergvall Nov 13 '12 at 14:59
  • Hi,Could you understand what i said?Do you think the expression is clear enough?Because i have received so many down vote since i post this problem. – Chailie Nov 14 '12 at 03:48

2 Answers2

0

Try making it;

NSNumber * currentPer;

and use numberWithFloat:(float)value of NSNumber to assign float values to NSNumber.

and when getting it use

[currentPer floatValue];

Here is brief explanation of why to use NSNumber.

tozlu
  • 4,667
  • 3
  • 30
  • 44
  • OK,Thanks,I will try it,Could you tell me why NSFloat is ok but float is not ok? – Chailie Nov 15 '12 at 12:06
  • If you insist on using float and not NSNumber, use an assigned property, try it and let me know if it works @property (readwrite, assign) float currentPer; – tozlu Nov 15 '12 at 15:33
  • HI,I just read the article ,But i still can't understand the actual reason!Here if my understanding :When i defined "initWithTarge:layer action:@selector(processPanGesture:)...",It equals [layer processPanGesture],So actually,It is just call GameLayer's processPanGesture() in due time.But the currentPer is GameLayer's member variable and layer is GameLayer's instance,So why layer can't visit and change itself member variable currentPer's value?In your option,It seems once change primitive type to object type,It will be ok?But why?What's substantial difference between float and NSNumber? – Chailie Nov 15 '12 at 15:35
  • I suppose the reason is about the usage of pointers and variables. While the other properties are pointers, _currentPer_ is simply a variable that is not directly accessible via GameLayer's instance. Either you make it a pointer, or you declare it in a way that each instance of the object has its own _float currentPer_ address. – tozlu Nov 15 '12 at 16:03
  • But the "_strengthBar" is not primitive type,You could see the screen shot that i uploaded ,It is CCProgressTime instance.I still can't change _strengthBar.percentage in call back function processPanGesture(). – Chailie Nov 16 '12 at 03:50
  • If the above provided answer is not your solution, please do not mark it as answer. Provide your own answer instead. – tozlu Nov 18 '12 at 12:18
0

I figure out the reason finally:It is because of the GameLayer instance A that used to register the PanGestureRecognizer and the GameLayer instance B which used to show the screen is not same instance.Such that processPanGesture() function is just change instance A's currentPer,While what i print is instance B's currentPe

Chailie
  • 451
  • 2
  • 11
  • 24