How to animate enemy with tween tasks in phaser 3 - phaser-framework

I have seen animated enemies in simple games that will chase you, attack you, or perform a sort of task.
In phaser 3, how do I give a sprite multiple tasks using tweens?
For now, I have only a tween that performs a really simple task
gameState.enemy.move = this.tweens.add({
targets: gameState.enemy,
x: 600,
ease: 'Linear',
duration: 1800,
repeat: -1,
yoyo: true
});
Also, what exactly is a tween? Can a sprite have multiple tweens? Can a tween help perform multiple tasks?
I found this website but did now understand..
https://rexrainbow.github.io/phaser3-rex-notes/docs/site/tween/

A Tween is just a "function", that changes properties (only numeric) values of an object (or multiple objects), from a start value to an end value, over a specific duration. (the object doesn't even need to be a phaser sprite or image or ..., as shown in the example below)
And Yes, you can change multiple values in one tween (check example below).
Here a very simple example, with a basic Javascript object:
var config = {
type: Phaser.AUTO,
width: 400,
height: 200,
backgroundColor: '#2d2d2d',
parent: 'phaser-example',
scene: {
create
}
};
var game = new Phaser.Game(config);
var simpleObject = {value: -1, value2: -2}
function create ()
{
console.info('Initial value', JSON.stringify(simpleObject))
this.tweens.add({
targets: simpleObject,
value: {from:0, to: 100},
value2: {from: 10, to: 200},
duration: 3000,
delay: 500,
onUpdate: _=> console.info('On Update', JSON.stringify(simpleObject)),
onComplete: _=> console.info('After Tween', JSON.stringify(simpleObject))
});
}
<script src="https://cdn.jsdelivr.net/npm/phaser#3.55.2/dist/phaser.js"></script>

Related

JointJS: How can you create a oneSide link with 2 different sides

I am trying to create a link that links from one shape with a fixed side to another with a different fixed side. Is this possible?
link.router('oneSide', {
side: 'top',
padding: 30
});
https://resources.jointjs.com/docs/jointjs/v3.1/joint.html#routers.oneSide
I'm not sure 'oneSide' should even be in the routers because in the documentation the router only defines the middle points of the path not the start and end.
In either case, the router function must return an array of route points that the link should go through (not including the start/end connection points). The function is expected to have the form function(vertices, args, linkView)
I found out that in defining the links you can specify the anchor which is basically the start and end point. For the shapes there some built-in anchors. We have to use 'left' and 'right' for this case.
Here is my code. It's in TypeScript but I'm sure you can convert it to JS easily if you need to.
makeLink(parentElementLabel, childElementLabel) {
return new joint.shapes.standard.Link({
source: {
id: parentElementLabel,
anchor: {
name: 'right'
}
},
target: {
id: childElementLabel,
anchor: {
name: 'left'
}
},
router: {
name: 'normal'
},
attrs: {
line: {
stroke: 'black',
cursor: 'default',
},
wrapper: {
cursor: 'default'
}
},
// smooth: true,
});
}

Highcharts - Hide child labels in a multiple levels and multiple layouts treemap

On highcharts, I have a treemap with 2 levels, each with a different layout algorithm. Now I want to limit what we can see to the current level. It means that on level 1, I don't want to see the labels of level 2, which would only appear when drilling down (and the labels of level 1 would disappear).
I know it is easy with levelIsConstant: false, but this only works with 1 level, and I use 2 because I need different layouts.
Here is the link to what I currently have:
series: [{
type: "treemap",
allowDrillToNode: true,
alternateStartingDirection: true,
levels: [{
level: 1,
layoutAlgorithm: 'squarified',
dataLabels: {
enabled: true,
align: 'left',
verticalAlign: 'top',
style: {
fontSize: '15px',
fontWeight: 'bold'
}
}
}, {
level: 2,
layoutAlgorithm: 'stripes',
color: 'blue'
}],
//...
http://jsfiddle.net/dhfera2y/2/
I want all the names to be hidden, as well as the lines separating them.
EDIT: Using a rgba color on each level, I can hide the nodes below it, but I still cannot hide their label!
Thank you it is a smart idea for the labels issue, but as I say I cannot use levelIsConstant: false because I need a different layout for each level at every moment. With this solution both levels can have a different layout when I am on the top level, but I soon as I drill down I loose the correct layout for the new view.
Almost :-)
EDIT : Okay so I finally managed to do it. I think it is impossible to achieve this in the way I was trying to, which was using the parent option for each child point of the serie in order to determine the hierarchy of the tree. So instead of using one serie with a hierarchy, I use one serie for the top level which I link to several series for the level below.
I was able the do this thanks to the drilldown option.
I found the solution in the official documentation :
http://www.highcharts.com/docs/chart-concepts/drilldown
I adjusted the code and it was all right. Here is the solution I came up with (it is a different work from my first link) :
http://jsfiddle.net/ff964fog/47/
series: [{
type: 'treemap',
layoutAlgorithm: 'squarified',
borderWidth: 3,
data: modulesData
}],
drilldown: {
series: servicesSerie
},
I still have to adjust a few things (like the disappearance of the animations for the bottom level), but in the end I have exactly what I wanted !
My take on some settings you can use to achieve this (JSFiddle):
series: [{
type: "treemap",
allowDrillToNode: true,
levelIsConstant: false,
// ...
levels: [{
level: 1,
dataLabels: {
enabled: true
}
// ...
}, {
level: 2,
borderWidth: 0,
dataLabels: {
enabled: false
}
}],
data[{
//...
}]
}]
The level 2 settings apply only when viewing from level 1. When drilling down the new view is considered to be level 1 because of levelIsConstant: false.
Setting borderWidth: 0 in level 2 is only needed if you want to hide the grid of level 2 when viewing from level 1.
You can use a custom formatter for plotOptions.treemap.datalabels. The code bellow is an example that uses this.series.rootNode and this.point.parent and compare them to examine if the label should be shown or not:
plotOptions: {
treemap: {
dataLabels: {
formatter: function(data) {
if (this.point.parent == (this.series.rootNode || null)) {
return this.key;
}
}
}
}
}
You can use any other property that is available in formatter function. Just log (console.log) this and data in the formatter function to see all the properties available:
plotOptions: {
treemap: {
dataLabels: {
formatter: function(data) {
console.log(this, data);
}
}
}
}

Refresh ol.layer.heatmap KML every 2 Seconds and Update Layer

I use OpenLayers Heatmap and I want to refresh the KML Vector every 2 seconds. So I thought it would be possible just to delete the Layer, refresh the Layer and then add the Layer in the map again. But nothing worked so far.
Here is my script:
var vector = new ol.layer.Heatmap({
source: new ol.source.Vector({
url: 'tweets.php',
format: new ol.format.KML({
extractStyles: false
})
}),
blur: parseInt(6, 10),
radius: parseInt(4, 10)
});
var raster = new ol.layer.Tile({
source: new ol.source.Stamen({
minZoom: 3,
maxZoom: 8,
layer: 'toner'
})
});
var koordinate = 5.9;
var map = new ol.Map({
target: 'map',
controls: [] ,
interactions: ol.interaction.defaults({
dragging: false,
dragPan: false
}),
view: new ol.View({
center: ol.proj.transform([10.5 , 51.0], 'EPSG:4326', 'EPSG:3857'),
minZoom: koordinate,
maxZoom: koordinate,
zoom: koordinate
})
});
map.addLayer(raster);
map.addLayer(vector);
blur.addEventListener('input', function() {
vector.setBlur(parseInt(blur.value, 10));
});
radius.addEventListener('input', function() {
vector.setRadius(parseInt(radius.value, 10));
});
Edit: This was my best solution for the problem but i don't worked.
setInterval(function() {
vector.loaded = false;
vector.setVisibility(true);
vector.redraw({ force: true });
}, 2000);
I've also tried everything with SetInterval, but everytime it was not correct or it was wrong.
I think the ol.layer.heatmap makes it difficult to solve the problem.
ol.layer.Heatmap doesn't provide a "redraw" method.
ol.Map does offer a render method though, which you should be using. consider the Dynamic data example for more insight: http://openlayers.org/en/v3.5.0/examples/dynamic-data.html

flot.js - points don't match up with x-axis, null data set, legend overlapping

Just started using flot.js for the first time and have a few problems/questions. The chart displays fine minus a few things that are bothering me.
1 - The ticks and dates don't seem to line up properly. For example, in the current chart I am working on there are only two entries for two different days. The actual data value (point) is not above the date it corresponds with for some reason. See the image here
2 - Is there any way to make the y axis larger than the dataset to prevent the lines from overlapping the legend? In this example it is fine, but I have seen others where the data overlaps the legend.
3 - How to handle no data? I plan to expand on this to allow the user to select start/end dates... so if there are no results is there a nice way to return this and/or possibly show a message in the chart? The main thing here is the script doesn't break when there is no data.
JS :
//get the data
$.ajax({
url: "/process/chart_main.php",
type: "POST",
dataType: "json",
success: onDataReceived
});
function onDataReceived(series) {
console.log(series);
// Load all the data in one pass; if we only got partial
// data we could merge it with what we already have.
data = series;
var options = {
series: {
lines: {
show: true,
lineWidth: 2,
fill: true,
fillColor: {
colors: [{
opacity: 0.05
}, {
opacity: 0.01
}
]
}
},
points: {
show: true
},
shadowSize: 2
},
grid: {
hoverable: true,
clickable: true,
tickColor: "#eee",
borderWidth: 0
},
//colors: ["#d12610", "#37b7f3", "#52e136"],
xaxis: {
tickSize: [1, "day"],
mode: "time",
timeformat: "%m-%d-%Y"
},
yaxes: [{
position: "left"
}],
legend: {
position: "ne"
}
};
$.plot("#flot_chart", data, options);
}
PHP:
//actual select removed for this display
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($results)
{
foreach($results as $row)
{
$data1[] = array( strtotime($row['date']) * 1000, $row['day_count'] );
}
//send our data values to $mergedData, add in your custom label and color
$mergedData[] = array('label' => "Events" , 'data' => $data1, 'color' => '#37b7f3');
}
// return result array to ajax
echo json_encode($mergedData);
Probably the ticks are at midnight (07-09-2014 00:00:00) while the data points are not. If you want to line them up remove the time from your datapoints or give flot the ticks with the same time as your datapoints as an array instead of the tickSize option (see here, you have to give the ticks as timestamps like in the datapoints):
xaxis: {
ticks: [1405942268798, 1405942368798, ...]
}
Use the min and max properties for the axis options (see same link as above):
xaxis: {
min: 1405942268798,
max: 1405942368798
}
flot doesn't "break" when there is no data, it just shows an empty chart. If you want to show an additional message, you can check for yourself before calling $.plot():
if (data.length == 0) {
alert('No data');
}

How to disable the on-hover color change in Highcharts?

I am using the column charts for my project. I have written a custom function that colors each bar of the chart based upon its y value. This works fine when I initialize the chart. As I hover over the chart, the color of the bar goes back to the default and my custom colors never return.
I have tries disabling on hover but that doesn't seem to work. I don't want the color to change even when hovered over the bar. Any suggestions?
You are looking for this option:
plotOptions: {
series: {
states: {
hover: {
enabled: false
}
}
}
},
Fiddle here.
EDITS
Instead of modifying the SVG directly to set your colors, do it within the api:
var max = 200;
var seriesData = $.map([107, 31, 635, 203, 2],function(datum, i){
return {
color: datum > max ? 'red' : '#2f7ed8',
y: datum
};
});
$('#container').highcharts({
chart: {
type: 'bar'
},
tooltip: {
valueSuffix: ' millions'
},
series: [{
name: 'Year 1800',
data: seriesData
}]
});
New fiddle.
You are updating color in a wrong way, use point.update() instead: http://jsfiddle.net/CaPG9/8/
$('#container').highcharts({
chart: {
type: 'bar'
},
tooltip: {
valueSuffix: ' millions'
},
series: [{
name: 'Year 1800',
data: [107, 31, 635, 203, 2]
}]
},function(chart){
var max = 200;
$.each(chart.series[0].data,function(i,data){
if(data.y > max)
data.update({
color:'red'
});
});
});

Resources