CCControlExtension icon indicating copy to clipboard operation
CCControlExtension copied to clipboard

CCSlider problems

Open njt1982 opened this issue 10 years ago • 9 comments

Hi,

Just been trying out the slider as a volume control for my game (Cocos2D iPhone 2.1). It, mostly, works REALLY well...

        slider = [CCControlSlider sliderWithBackgroundSprite:[CCSprite spriteWithSpriteFrameName:@"Slider-Bar.png"]
                                                               progressSprite:[CCSprite spriteWithSpriteFrameName:@"Slider-Bar-Progress.png"]
                                                                  thumbSprite:[CCSprite spriteWithSpriteFrameName:@"Slider-Pin.png"]];

        [slider setMinimumValue:0.0f];
        [slider setMaximumValue:1.0f];

        NSNumber *defaultVolume = [SDCloudUserDefaults objectForKey:@"OptionsVolume"];
        [self updateVolume:((defaultVolume != nil) ? [defaultVolume floatValue] : 1.0f) setSliderValue:YES];


        [slider setBlock:^(CCControlSlider *sender, CCControlEvent event) {
            [self updateVolume:sender.value setSliderValue:NO];
        } forControlEvents:CCControlEventValueChanged];

        [slider setAnchorPoint:ccp(0.5f, 1.0f)];
        [slider setPosition:ccp(winSize.width * 0.5f, winSize.height * 0.66f)];
        [self addChild:slider];


        CCLabelBMFont *volumeLabel = [CCLabelBMFont labelWithString:@"Volume"
                                                            fntFile:@"fntFipps-medium.fnt"
                                                              width:winSize.width
                                                          alignment:kCCTextAlignmentCenter];
        [volumeLabel setAnchorPoint:ccp(0.5f, 0.0f)];
        [volumeLabel setPosition:ccpAdd(slider.position, ccp(0.0f, 8.0f))];
        [self addChild:volumeLabel];

On my iPhone5, this works perfectly. On all simulators, this works perfectly.

On my iPad3 and my friends' iPhone 5S' the framerate while dragging is terrible. It only seems to update when you stop dragging your finger, or you do it slowly.

A separate issue is that it works when the options menu scene loads the first time, however after playing the game and quitting back to the menu, the option scene loads but I cannot drag the button on the slider. The init code above runs and it looks like the block gets added, but it seems no touch events are getting sent to it. The Back menu button on the same page works though, so the layer/scene is getting touches.

Any thoughts?

njt1982 avatar Feb 21 '14 09:02 njt1982

Hi,

Unfortunately I have no iPhone 5S neither iPads 3+, so I can't test with these devices.

About your first issue, have you tried to run the instruments to target where the problem comes from? Have you tried to replace the setBlock:forControlEvents: by a addTarget:action:forControlEvents instead (just to check whether the problem appears)?

Does your second problem occurred on all your devices or also on the iPhone 5S and iPad 3+?

I have not a lot of time for the moment, so I don't know when I'll be able to check that... :! I'll try to do my best, but without any guarantee.

Thank you for the report. :+1:

yannickl avatar Feb 21 '14 10:02 yannickl

I have not yet - I found this bug late last night and confirmed it early this morning. I will try hooking up my iPad 3. The strange thing is, the music continues to play smoothly and, while dragging, the volume changes smoothing (implying the block is fired for every touch movement) - it just seems like the rendering is delayed... Very strange! I'll check on my iPad to see if there is an FPS drop or a CPU spike.

I have tried both block and target/selector (that was my first suspicion too).

The 2nd problem occurs on all devices. The problem occurs for both block and target/selector. They just dont fire, its like the touch is not making it to the node.

I have a main menu and I push/pop the OptionsMenu scene when I go to it or click Back. Now I can go and leave the options as many times as I like, it only breaks if I go to a game, then back to the menu. I suspect my game may be doing something (maybe it's stealing the touch?!).... I wonder if its touch priority related?!

njt1982 avatar Feb 21 '14 10:02 njt1982

Indeed it's very strange, may be the problem comes from the CPU/GPU changes. It would be great if you could make some tests on these devices.

Concerning the 2nd problem, may be there is something wrong with the scene transition or, as you mentioned, with the touch dispatching or with a sprite which overlap the slider? Have you tried to log whether the slider is receiving the touches?

yannickl avatar Feb 21 '14 13:02 yannickl

I have tried putting breakpoints inside the block (they dont get hit) - however I have not tried specifically on any touch handlers.

njt1982 avatar Feb 21 '14 15:02 njt1982

Ok just tried... ccTouchBegan and ccTouchMoved do not fire.

njt1982 avatar Feb 21 '14 15:02 njt1982

Ah ha! I have solved the "Stuck" slider problem... In my gameLayer.m I add a targeted delegate like this:

        [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];

At this very second, I do not know why I used -1... I suspect it was so that when the PauseMenu.m scene was pushed over the GameLayer, the touches didn't apply to both layers.

Of course, the question now is why that effects touches in a completely different scene, even when that scene is pop'd and is not longer running! (onExit should release that delegate)...

EDIT: I have resolve this by changing my code to something which feels like a better use of the API...

        [self setTouchMode:kCCTouchesOneByOne];
        [self setTouchEnabled:YES];

(Probably dont need the 2nd line, but its nice to be explicit sometimes! ;) )

njt1982 avatar Feb 21 '14 15:02 njt1982

Just tried to run it on my iPad 3 while tethered to XCode... The game on options menu, idle, does 10% CPU and 60FPS (which is the cap). Dragging the slider side to side, rapidly, the CPU goes to ~30% but the FPS drops to 5-8FPS.

This is a screengrab of the status toolbar thingy in XCode. screen shot 2014-02-21 at 15 26 38

Any ideas where I can debug next to help move this along?

njt1982 avatar Feb 21 '14 15:02 njt1982

Running on my iPhone 5, same test as above, I get about the same CPU usage pattern, but the FPS remains at 60FPS.

screen shot 2014-02-21 at 15 30 23

njt1982 avatar Feb 21 '14 15:02 njt1982

@njt1982

[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES]; Here you retain the delegate, you ask to have the higher priority (lower number = higher priority), and you avoid the touch propagation. So you need to remove the delegate on the onExit to be sure that your scene will be released. May be the problem comes from elsewhere. But that's weird, it should work...

Concerning the FPS drops, I don't know why and unfortunately I have old devices and so I works well for me. May be you should ask the question on the cocos2d forum by linking this issue. I'll also try to figure out the problem on my side.

yannickl avatar Feb 21 '14 15:02 yannickl