使用cocos2d-x-3.13.1
我创建了<?xml version="1.0" encoding="UTF-8"?>
<?language javascript?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.util.* ?>
<?import javafx.scene.*?>
<?import javafx.scene.control.* ?>
<?import javafx.scene.layout.* ?>
<?import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory?>
<HBox xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"
alignment="BASELINE_LEFT" spacing="15">
<fx:script>
function handleCheckBoxAction(event) {
mySpinner.disable = (! mySpinner.disabled);
}
</fx:script>
<children>
<CheckBox fx:id="myCheckbox" text="Disable?" mnemonicParsing="false" onAction="handleCheckBoxAction(event);" />
<Spinner fx:id="mySpinner">
<valueFactory>
<SpinnerValueFactory.IntegerSpinnerValueFactory min="0" max="10"/>
</valueFactory>
</Spinner>
</children>
</HBox>
,其中88个按钮在游戏关卡中开始不同。
我的功能:用户可以启动所选级别。但是,如果初始点击位置不在按钮上,则只能滚动。
我想要的功能:如果初始触摸位置在按钮上,用户可以滚动ScrollView
。
创建
ScrollView
ScrollView
添加按钮
containerLayer = cocos2d::LayerColor::create();
containerLayer->setContentSize(Size(visibleSize.width, visibleSize.height * 4.5));
containerLayer->setPosition(Point(0, -visibleSize.height * 3.7));
auto scrollView = cocos2d::extension::ScrollView::create();
scrollView->setContentSize(Size(containerLayer->getContentSize().width, containerLayer->getContentSize().height));
scrollView->setPosition(Point(visibleSize.width * 0.05, visibleSize.height * 0.05));
// set the scroll-direction for scroll-view
scrollView->setDirection(cocos2d::extension::ScrollView::Direction::VERTICAL);
scrollView->setViewSize(Size(visibleSize.width * 0.90, visibleSize.height * 0.8));
// set the content offset of the scrollview
scrollView->setContentOffset(Vec2(0, 0));
scrollView->setTouchEnabled(true);
// add / set the container-layer to the scrollview.
scrollView->setContainer(containerLayer);
// add scroll-view to your scene-layer.
this->addChild(scrollView, 100);
答案 0 :(得分:1)
一种肮脏的方法是创建按钮的子类,您可以在其中存储ScrollView
的指针作为成员变量:
auto button = _YourBtnClass_::create(pScrollView);
并且您需要禁用该ScrollView
的触摸:
pScrollView->setTouchEnabled(false);
最后,在 YourBtnClass 中覆盖onTouchBegan
,onTouchMoved
,onTouchEnded
和onTouchCancelled
,您可以在其中调用pScrollView的相应函数< / p>
答案 1 :(得分:1)
而不是cocos2d::extension::ScrollView
我使用了cocos2d::ui::ScrollView
,cocos2d::ui::Button
,而且符合我对ScrollView
的要求。
scrollView = cocos2d::ui::ScrollView::create();
scrollView->setDirection(ui::ScrollView::Direction::VERTICAL);
scrollView->setContentSize(Size(visibleSize.width * 0.9, visibleSize.height * 0.8)); // What user see
scrollView->setInnerContainerSize(Size(visibleSize.width * 0.9, visibleSize.height * 0.8 * 4.5));
scrollView->setBounceEnabled(true);
scrollView->setAnchorPoint(Point(0.5, 0.5));
scrollView->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height * 0.45 + origin.y));
int level = 1;
for (int h = 0; h < 22; h++) {
for (int w = 0; w < 4; w++) {
auto button = ButtonLevel::create(this, Point(scrollView->getInnerContainerSize().width / 5 * (w + 1), scrollView->getInnerContainerSize().height - scrollView->getInnerContainerSize().height / 22 * (h + 1) + scrollView->getInnerContainerSize().height / 44), level, canBePlayed);
button->addClickEventListener([=](Ref* _sender)
{
auto scene = GameScene::createSceneWithLevel(level);
Director::getInstance()->replaceScene(TransitionFade::create(1.0, scene, Color3B(0, 0, 0)));
});
scrollView->addChild(button);
level++;
}
}
this->addChild(scrollView, 100);
还需要#include "ui/CocosGUI.h"