Does anyone know why when I click on different radio values multiple times, my Dynamic Total value changes drastically?
The code below should automatically select the column of checkboxes according to the radio buttons. Not sure why there would be miscalculations.
switch($scope.ChangeAll.name) {
case 'x':
row.xBox = true;
row.yBox = false;
row.zBox = false;
break;
case 'y':
row.xBox = false;
row.yBox = true;
row.zBox = false;
break;
case 'z':
row.xBox = false;
row.yBox = false;
row.zBox = true;
break;
default:
row.xBox = true;
row.yBox = false;
row.zBox = false;
}
https://plnkr.co/edit/bAnz9kCuKWGIfWD4lEO2?p=preview
This tweak should do it.
AngularJS Controller (relevant code change):
$scope.updateSelection = function(value) {
switch ($scope.ChangeAll.name) {
case 'x':
angular.forEach($scope.myData, function(row, idx) {
row.xBox = true;
row.yBox = false;
row.zBox = false;
});
break;
case 'y':
angular.forEach($scope.myData, function(row, idx) {
row.xBox = false;
row.yBox = true;
row.zBox = false;
});
break;
case 'z':
angular.forEach($scope.myData, function(row, idx) {
row.xBox = false;
row.yBox = false;
row.zBox = true;
});
break;
default:
angular.forEach($scope.myData, function(row, idx) {
row.xBox = true;
row.yBox = false;
row.zBox = false;
});
}
};
And the all important Plunker update, https://plnkr.co/edit/QOkoG9pC7gETvZ4I7Xkq?p=preview.
Related
I'm making a snake game in React and it works fine but I have defined a switch case, where you can control the snake with the arrow keys. But there's a problem, when the snake is going to one direction I don't want it to be able to go to the opposite direction.
as in prevent changing direction along same axis, ie left if currently moving right, down if currently moving up
Now I'm wondering how can I define an If statement in my switch case for it?
My code so far looks like this :
//handle user input
const handleKeyPress = (e) => {
e = e || window.event;
setStarted(true);
switch (e.keyCode) {
case 38:
setDirection('UP');
break;
case 40:
setDirection('DOWN');
break;
case 37:
setDirection('LEFT');
break;
case 39:
setDirection('RIGHT');
break;
default:
break;
}
};
//moving the snake
const moveSnake = () => {
//take temporary positions
let tempSnakePosition;
let dir;
//get fresh cordinates
setSnakeCordinates((prev) => {
tempSnakePosition = [...prev];
return prev;
});
setDirection((prev) => {
dir = prev;
return prev;
});
//find head
let tempHead = tempSnakePosition[tempSnakePosition.length - 1];
//change head
let l = tempHead.left;
let t = tempHead.top;
switch (dir) {
case 'RIGHT':
if ('LEFT' && 'UP')
l = tempHead.left + size
break;
case 'LEFT':
if ('RIGHT')
l = tempHead.left - size;
break;
case 'DOWN':
t = tempHead.top + size;
break;
case 'UP':
t = tempHead.top - size;
break;
default:
break;
}
const [prevDir, setPrevDir] = useState('');
const handleKeyPress = (e) => {
switch (e.keyCode) {
case 38:
if(prevDir !== 'DOWN') {
setDirection('UP');
}
break;
case 40:
if(prevDir !== 'UP') {
setDirection('DOWN');
break;
}
case 37:
if(prevDir !== 'RIGHT') {
setDirection('LEFT');
}
break;
case 39:
if(prevDir !== 'LEFT') {
setDirection('RIGHT');
break;
}
default:
break;
}
}
setDirection((prev) => {
dir = prev;
setPrevDir(prev);
return prev;
});
Try using you dir variable like this:
//handle user input
const handleKeyPress = (e) => {
e = e || window.event;
setStarted(true);
switch (e.keyCode) {
case 38:
if(dir !== 'DOWN')
setDirection('UP');
break;
case 40:
if(dir !== 'UP')
setDirection('DOWN');
break;
case 37:
if(dir !== 'RIGHT')
setDirection('LEFT');
break;
case 39:
if(dir !== 'LEFT')
setDirection('RIGHT');
break;
default:
break;
}
};
i have an error in c# of NullReferenceException: Object reference not set to an instance of an object
SimpleJSON.JSONNode.Parse (System.String aJSON) (at Assets/Scenes/scripts/SimpleJSON.cs:553)
I found this post in the link below but i didn't understand the solution
SimpleJSON returns null when handling a call from Postmen
Can any one explain it for me, please?
the error is :
NullReferenceException: Object reference not set to an instance of an object
SimpleJSON.JSONNode.Parse (System.String aJSON) (at Assets/Scenes/scripts/SimpleJSON.cs:553)
SimpleJSON.JSON.Parse (System.String aJSON) (at Assets/Scenes/scripts/SimpleJSON.cs:1363)
Control.Update () (at Assets/Control.cs:123)
knowing that i'm sending json data using websocket and i'm receiving it in the console unity. i use simpleJSON script to deserialize the data.
here where the error is provided in line JSONNode root = JSON.Parse(last_message); :
private void Update()
{
if (ws == null)
{
return;
}
List<GameObject> object_to_keep = new List<GameObject>();
JSONNode root = JSON.Parse(last_message);
JSONNode Face = root["streams"][0]["objects"][0];
{
int id = Face["mono"]["id"].AsInt;
JSONNode position = Face["mono"]["position"];
Vector3 pos = new Vector3(position["x"].AsFloat, position["y"].AsFloat, position["z"].AsFloat);
GameObject mono = GetMonoObject();
WintualFaceController win_controller = mono.GetComponent<WintualFaceController>();
win_controller.face_id = id;
if(mono)
{
mono.transform.localPosition = pos;
object_to_keep.Add(mono);
}
else
print("Mono not found");
}
}
The error in SImpleJSON script wan in line: while (i < aJSON.Length)
public static JSONNode Parse(string aJSON)
{
Stack<JSONNode> stack = new Stack<JSONNode>();
JSONNode ctx = null;
int i = 0;
StringBuilder Token = new StringBuilder();
string TokenName = "";
bool QuoteMode = false;
bool TokenIsQuoted = false;
while (i < aJSON.Length)
{
switch (aJSON[i])
{
case '{':
if (QuoteMode)
{
Token.Append(aJSON[i]);
break;
}
stack.Push(new JSONObject());
if (ctx != null)
{
ctx.Add(TokenName, stack.Peek());
}
TokenName = "";
Token.Length = 0;
ctx = stack.Peek();
break;
case '[':
if (QuoteMode)
{
Token.Append(aJSON[i]);
break;
}
stack.Push(new JSONArray());
if (ctx != null)
{
ctx.Add(TokenName, stack.Peek());
}
TokenName = "";
Token.Length = 0;
ctx = stack.Peek();
break;
case '}':
case ']':
if (QuoteMode)
{
Token.Append(aJSON[i]);
break;
}
if (stack.Count == 0)
throw new Exception("JSON Parse: Too many closing brackets");
stack.Pop();
if (Token.Length > 0 || TokenIsQuoted)
{
ParseElement(ctx, Token.ToString(), TokenName, TokenIsQuoted);
TokenIsQuoted = false;
}
TokenName = "";
Token.Length = 0;
if (stack.Count > 0)
ctx = stack.Peek();
break;
case ':':
if (QuoteMode)
{
Token.Append(aJSON[i]);
break;
}
TokenName = Token.ToString();
Token.Length = 0;
TokenIsQuoted = false;
break;
case '"':
QuoteMode ^= true;
TokenIsQuoted |= QuoteMode;
break;
case ',':
if (QuoteMode)
{
Token.Append(aJSON[i]);
break;
}
if (Token.Length > 0 || TokenIsQuoted)
{
ParseElement(ctx, Token.ToString(), TokenName, TokenIsQuoted);
TokenIsQuoted = false;
}
TokenName = "";
Token.Length = 0;
TokenIsQuoted = false;
break;
case '\r':
case '\n':
break;
case ' ':
case '\t':
if (QuoteMode)
Token.Append(aJSON[i]);
break;
case '\\':
++i;
if (QuoteMode)
{
char C = aJSON[i];
switch (C)
{
case 't':
Token.Append('\t');
break;
case 'r':
Token.Append('\r');
break;
case 'n':
Token.Append('\n');
break;
case 'b':
Token.Append('\b');
break;
case 'f':
Token.Append('\f');
break;
case 'u':
{
string s = aJSON.Substring(i + 1, 4);
Token.Append((char)int.Parse(
s,
System.Globalization.NumberStyles.AllowHexSpecifier));
i += 4;
break;
}
default:
Token.Append(C);
break;
}
}
break;
default:
Token.Append(aJSON[i]);
break;
}
++i;
}
this is my json:
{
"cmd": "data",
"frame_time": "2021-06-30T09:04:16.464836+00:00",
"start_time": "2021-06-30T07:51:33.452079+00:00",
"streams": [
{
"name": "usb-0001:012.0-3",
"objects": [
{
"name": "face",
"mono": {
"id": "190",
"position": {
"x": "-0.012259",
"y": "0.059731",
"z": "0.707695"
}
},
"multi": [
{
"id": "190",
"position": {
"x": "-0.012263",
"y": "0.059753",
"z": "0.707151"
}
}
]
}
]
}
]
}
Please forgive my noob-ness.
So, i figured out how to create two separate switch loops, in order to access two separate values. My trouble now, is that I don't know how to write a function that would allow me to concatenate the results of the two switch values.
var select = document.getElementById('weather');
var para = document.getElementById('alf');
var door = document.getElementById('dance');
var mort = document.getElementById('tim');
document.getElementById('weather').addEventListener('change',
setWeather);
document.getElementById('dance').addEventListener('change',
setDance);
function setWeather() {
var choice = this.value;
switch(choice) {
case 'sunny':
para.textContent = "fly me to the Moon";
break;
case 'rainy':
para.textContent = "let me sail amongst the stars";
break;
case 'snowy':
para.textContent = "life on Jupiter and Mars";
break;
default:
para.textContent = "";
}
}
function setDance() {
var art = this.value;
switch(art) {
case 'hail':
mort.textContent = "in other words";
break;
case 'rail':
mort.textContent = "darling kiss me";
break;
case 'cat':
mort.textContent = "please be true";
break;
default:
mort.textContent = "";
}
}
function result() {
weatherVal = weather.value;
danceVal = dance.value;
para.textContent = weatherVal + danceVal;
}
I tried to use the above code "function result()" to write the function to create the concatenation, but no success. I feel like the answer is right in front of me, but I'm just not quite sure what's going on.
You could make choice and art global variables and then use them in result like this:
var select = document.getElementById('weather');
var para = document.getElementById('alf');
var door = document.getElementById('dance');
var mort = document.getElementById('tim');
document.getElementById('weather').addEventListener('change', setWeather);
document.getElementById('dance').addEventListener('change', setDance);
var choice;
var art;
function setWeather() {
choice = this.value;
switch(choice) {
case 'sunny':
para.textContent = "fly me to the Moon";
break;
case 'rainy':
para.textContent = "let me sail amongst the stars";
break;
case 'snowy':
para.textContent = "life on Jupiter and Mars";
break;
default:
para.textContent = "";
break;
}
}
function setDance() {
art = this.value;
switch(art) {
case 'hail':
mort.textContent = "in other words";
break;
case 'rail':
mort.textContent = "darling kiss me";
break;
case 'cat':
mort.textContent = "please be true";
break;
default:
mort.textContent = "";
break;
}
}
function result() {
para.textContent = choice + art;
}
This is my first post.
if (condition) {
trace("called");
p[1].visible = false;
j[1].visible = false;
}
With the code above "called" was printed in console but the both objects (buttons) still visible. Then when I try to put the set visibility (p[1].visible = false; and j[1].visible = false;) out from condition, it's work well.
I wonder what the problem here and how can I do set visibility with some condition?
[EDIT]
This is my actual source code. The code snippet above just to simple my question.
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.events.Event;
stop();
var isInit:Boolean;
var val:Array;
var p:Array;
if (!isInit)
{
isInit = initial();
}
function initial():Boolean
{
trace("init");
val = new Array();
val[1] = 0;
val[2] = 0;
val[3] = 0;
val[4] = 0;
val[5] = 0;
val[6] = 0;
pinit();
jinit();
ainit();
binit();
cinit();
dinit();
einit();
return true;
}
function pinit():void
{
p = new Array();
p[1] = p1;
p[2] = p2;
p[3] = p3;
}
// event listener works
p[1].addEventListener(MouseEvent.CLICK, function (event:MouseEvent):void {
p[1].visible = false;
});
p[2].addEventListener(MouseEvent.CLICK, function (event:MouseEvent):void {
p[2].visible = false;
});
if (isInit)
{
trace("set visibility"); // this is printed as well
var i:int;
for (i = 1; i <= 3; i++)
{
setVisibility(i, val[i]);
}
}
function setVisibility(num:int, val:int):void
{
if (val==0)
{
p[num].visible = true;
j[num].visible = true;
}
else if (val==1)
{
trace("one");
p[num].visible = false;
j[num].visible = false;
}
else if (val==2)
{
trace("two");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
}
else if (val==3)
{
trace("three");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
}
else if (val==4)
{
trace("four");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
c[num].visible = false;
}
else if (val==5)
{
trace("five");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
c[num].visible = false;
d[num].visible = false;
}
}
The ainit, binit, cinit, etc functions are in another layer (on same frame) because the objects are there. The "one", "two", "three", "four", or "five" is printed but the visibility not set correctly.
Just to make your code simpler and easier to read.
With your val array you can populate it like this
val = new Array (0,0,0,0,0,0,0);
Yes this has seven numbers as an array starts at 0. You don't have to reference it but I would populate it just to make it look nicer.
I also changed the if else bit at the end to this:
switch (val){
case 0:
p[num].visible = true;
j[num].visible = true;
break;
case 1:
trace("one");
p[num].visible = false;
j[num].visible = false;
break;
case 2:
trace("two");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
break;
case 3:
trace("three");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
break;
case 4:
trace("four");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
c[num].visible = false;
break;
case 5:
trace("five");
p[num].visible = false;
j[num].visible = false;
a[num].visible = false;
b[num].visible = false;
c[num].visible = false;
d[num].visible = false;
break;
}
As to your actual problem I don't think you can change a button from an array as it would become a copy of the button not the button itself (Hope that makes sense). I would still have the arrays but instead of the button visibility I'd use Booleans. So that at the end you could put:
btnButton.visible = p1;
Hope that's all Okay and of some use.
I'm building a top down (birds eye view) game in flash, a fairly simple concept, a la early Legend of Zelda or Final Fantasy games. I have the level built and the character added and moveable, with a scrolling camera, yet one problem vexes me. Hittesting.
The level is created using an array- a simple [1,1,1]; array, where it checks to see if a value in the array matches, then creates a simple square Sprite and adds it to a larger Sprite that holds every item. Finally, the player is then added to the stage and can be controlled using the arrow keys. I'm using this as a movement system:
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkUp);
stage.addEventListener(Event.ENTER_FRAME, update);
function checkDown(e:KeyboardEvent):void {
switch (e.keyCode) {
case 65 :
leftDown = true;
break;
case 37 :
leftDown = true;
break;
case 68 :
rightDown = true;
break;
case 39 :
rightDown = true;
break;
case 87 :
upDown = true;
break;
case 38 :
upDown = true;
break;
case 83 :
downDown = true;
break;
case 40 :
downDown = true;
break;
}
}
function checkUp(e:KeyboardEvent):void {
switch (e.keyCode) {
case 65 :
leftDown = false;
break;
case 37 :
leftDown = false;
break;
case 68 :
rightDown = false;
break;
case 39 :
rightDown = false;
break;
case 87 :
upDown = false;
break;
case 38 :
upDown = false;
break;
case 83 :
downDown = false;
break;
case 40 :
downDown = false;
break;
}
}
function update(e:Event):void {
if (upDown) {
if (genericHolder.y >= boundariesLevelOne[(room*4) - 4]) {
genericHolder.y = boundariesLevelOne[(room*4) - 4];
Player.y-=power;
}
if (genericHolder.y < boundariesLevelOne[(room*4) - 4]) {
genericHolder.y+=power;
}
Player.Stand.visible = false;
Player.Forwards.visible = false;
Player.Rights.visible = false;
Player.Lefts.visible = false;
Player.Backwards.visible = true;
playerDirection = 2;
}
if (downDown) {
if (genericHolder.y <= boundariesLevelOne[(room*4) - 3]) {
genericHolder.y = boundariesLevelOne[(room*4) - 3];
Player.y+= power;
}
if (genericHolder.y > boundariesLevelOne[(room*4) - 3]) {
genericHolder.y-=power;
}
Player.Stand.visible = false;
Player.Backwards.visible = false;
Player.Rights.visible = false;
Player.Lefts.visible = false;
Player.Forwards.visible = true;
playerDirection = 1;
}
if (leftDown) {
if (genericHolder.x >= boundariesLevelOne[(room*4) - 2]) {
genericHolder.x = boundariesLevelOne[(room*4) - 2];
Player.x -= power;
}
if (genericHolder.x < boundariesLevelOne[(room*4) - 2]) {
genericHolder.x+=power;
}
Player.Stand.visible = false;
Player.Backwards.visible = false;
Player.Forwards.visible = false;
Player.Rights.visible = false;
Player.Lefts.visible = true;
playerDirection = 4;
}
if (rightDown) {
if (genericHolder.x <= boundariesLevelOne[(room*4) - 1]) {
genericHolder.x = boundariesLevelOne[(room*4) - 1];
Player.x += power;
}
if (genericHolder.x > boundariesLevelOne[(room*4) - 1]) {
genericHolder.x-=power;
}
Player.Stand.visible = false;
Player.Backwards.visible = false;
Player.Forwards.visible = false;
Player.Lefts.visible = false;
Player.Rights.visible = true;
playerDirection = 3;
}
if (!upDown && !downDown && !leftDown && !rightDown) {
Player.Backwards.visible = false;
Player.Forwards.visible = false;
Player.Rights.visible = false;
Player.Lefts.visible = false;
Player.Stand.visible = true;
Player.Stand.gotoAndStop(playerDirection);
}
if (Player.Stand) {
Player.Stand.gotoAndStop(playerDirection);
}
}
TL;DR The background moves instead of the player, and movement is controlled by variables.
How can I Hittest against any of the sprites that I've added? At the moment, if I try to Hittest a specific Sprite, it tests agains the entire Sprite holder, not the specific Sprite I want.
Are you using the hittest method?
http://www.actionscriptmoron.com/AS3Tutorials/hittest-hittestobject/
What do you mean it tests again the entire sprite holder just get the reference of the contained object and pass it to the hittest method. it should work.