GamePlayKit GKObstacleGraph保存并加载

时间:2016-09-15 16:57:49

标签: gameplay-kit

GKObstacleGraph可以保存到文件中并从那里加载吗? 我找不到任何相关内容。

我很乐意为我的关卡保存并加载预先计算的图表。

到目前为止我已经尝试了

 NSArray * obstacles = [SKNode obstaclesFromNodePhysicsBodies:arrayOfBodies];
 _graph = [GKObstacleGraph graphWithObstacles:obstacles bufferRadius:[(BaseUnit *)[_units firstObject] size].width/2];
[NSKeyedArchiver archiveRootObject:_graph toFile:@"/Users/roma/Desktop/myGraph.graph"];

但这就是我得到的:

  

- [GKObstacleGraph encodeWithCoder:]:无法识别的选择器发送到实例0x6180000432d0

1 个答案:

答案 0 :(得分:0)

"Uncaught TypeError: Cannot read property 'format' of undefined dc.js:484 Uncaught TypeError: Cannot read property '1' of null header.js:3 Uncaught ReferenceError: dc is not defined (index):39 " GKGraph的子类,(从macOS 10.12开始,iOS 10和tvOS 10 )声明符合NSCoding协议。这意味着您可以使用NSKeyedArchiver(和NSKeyedUnarchiver)将一个序列化为数据或文件(并反序列化以从文件创建实例),就像支持 <!DOCTYPE html> <html> <head> <link type="text/css" rel="stylesheet" href="css/dc.css"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"> <script type="text/javascript" src="js/crossfilter.js" ></script> <script type="text/javascript" src="js/d3.js" ></script> <script type="text/javascript" src="js/dc.js"></script> </head> <body> <div class="container"> <script type="text/javascript" src="js/header.js"></script> <p>This example demonstrates assigning a threshold dynamically to a pie chart by creating a new dimension based on slider input.</p> <div id="inputSlider"> <div><input type="range" id="slideRange" value="0.5" min="0" max="1.0" step="0.1" onchange="updateSlider(this.value)"></div> Score threshold: <span id="sliderValue">0.5</label> </div> <div id="dc-coreAcc-piechart"></div> <div id="dc-score-barchart"></div> <script type="text/javascript"> var data = [ { "book": "A", "scores": 45 }, { "book": "B", "scores": 34 }, { "book": "C", "scores": 54 }, { "book": "D", "scores": 27 }, { "book": "E", "scores": 70 }, { "book": "F", "scores": 25 }, { "book": "G", "scores": 92 }, { "book": "H", "scores": 22 }, { "book": "I", "scores": 40 }, { "book": "J", "scores": 10 }, { "book": "K", "scores": 40 } ]; //## Create Chart Objects var scoreChart = dc.barChart("#dc-score-barchart") .xAxisLabel('book_id') .yAxisLabel('score'); var goodYesNoPieChart = dc.pieChart('#dc-coreAcc-piechart'); //### Create Crossfilter Dimensions and Groups var ndx = crossfilter(data); var all = ndx.groupAll(); var bookDimension = ndx.dimension(function (d) {return d.book;}), bookscoresGroup = bookDimension.group().reduceSum(function(d) {return d.scores;}); //## score bar chart scoreChart.width(320) .height(320) .dimension(bookDimension) .group(bookscoresGroup) .elasticY(true) .x(d3.scale.ordinal()) .xUnits(dc.units.ordinal) .colors(["orange"]) .yAxis().ticks(5); //## pie chart // reusable function to create threshold dimension function coreCount_from_threshold() { var scoreThreshold=document.getElementById('slideRange').value; scoreThreshold=parseFloat(scoreThreshold); if (isNaN(scoreThreshold)) { scoreThreshold=0.5 } return ndx.dimension(function (d) { var maxNumber=80; if (d.scores >maxNumber*scoreThreshold) { return 'High'; } else { return 'Low'; } }); } var coreCount = coreCount_from_threshold(); var coreCountGroup = coreCount.group(); goodYesNoPieChart .width(320) .height(320) .radius(120) .innerRadius(40) .dimension(coreCount) .title(function(d){return d.value;}) .group(coreCountGroup) .label(function (d) { if (goodYesNoPieChart.hasFilter() && !goodYesNoPieChart.hasFilter(d.key)) { return d.key + '(0%)'; } var label = d.key; if (all.value()) { label += '(' + Math.floor(d.value / all.value() * 100) + '%)'; } return label; }) dc.renderAll(); //## change slider score value to re-assign the data in pie chart function updateSlider(slideValue) { var sliderDiv = document.getElementById("sliderValue"); sliderDiv.innerHTML = slideValue; coreCount.dispose(); coreCount = coreCount_from_threshold(); coreCountGroup = coreCount.group(); goodYesNoPieChart .dimension(coreCount) .group(coreCountGroup); dc.redrawAll(); } </script> </div> </body> </html> 的任何其他对象一样}。

有关归档的一般信息(适用于任何GKObstacleGraph - 兼容类),请参阅Apple的Archives and Serializations Programming Guide

此外,在Xcode 8中(部署到macOS 10.12,iOS 10或tvOS 10时),您可以在可视化编辑器中创建NSCoding以与SpriteKit场景一起使用。当你这样做时,你使用GKScene类从NSCoding文件中加载SpriteKit场景和GK对象(不仅包括寻路图,还包括实体/组件信息)Xcode写道

在较旧的操作系统版本中,GKGraph系列不支持.sks。但是,重建GKGraph所需的所有信息都是公开可访问的。因此,您可以通过读取图形的缓冲区半径和障碍列表以及读取每个障碍物的顶点列表来实现自己的序列化。将该信息写入文件但是您喜欢...然后当您想要重建图形时,从您保存的顶点创建NSCoding s,并从这些障碍物和保存的缓冲区创建一个新的GKObstacleGraph半径。