set visibility trough property not working actionscript 3.0 - arrays

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.

Related

Is there any way to replace text in ChromiumBrowser like Chrome already has?

I'm making a web editor application using CefSharp WinForms library, but I couldn't find a way to replace text from CefSharp API.
There is a find method in WebBrowserExtensions but no replace method.
Question 1:
Does anyone know where replacing text method is in CefSharp?
Or there is no way to replace text in CefSharp? If yes, I need to find a detour for it.
Question 2:
There are yellow blocks marked found words when I try Find method, but those blocks are not a part of selection range of window object in HTML. Are those blocks made by native not web browser?
Answer:
I made "find and replace" function by myself using javascript, so if someone tries to do something like me then you can use below codes:
var lastsearchedNodeIndex = -1;
var lastSearchedTextIndex = -1;
var lastRange = null;
function getTextLengthFromStartTo(targetNodeIndex) {
var childNodes = editor.childNodes;
var textLength = 0;
if (targetNodeIndex >= childNodes.length) {
return editor.textContent.length;
}
for (var i = 0; i < targetNodeIndex; i++) {
if (childNodes[i].textContent != null) {
textLength += childNodes[i].textContent.length;
}
}
return textLength;
}
function getCurrentCaretIndex() {
var currentCaretIndex = 0;
var doc = editor.ownerDocument || editor.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
if (typeof win.getSelection != "undefined") {
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = win.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(editor);
preCaretRange.setEnd(range.endContainer, range.endOffset);
currentCaretIndex = preCaretRange.toString().length;
}
} else if ( (sel = doc.selection) && sel.type != "Control") {
var textRange = sel.createRange();
var preCaretTextRange = doc.body.createTextRange();
preCaretTextRange.moveToElementText(editor);
preCaretTextRange.setEndPoint("EndToEnd", textRange);
currentCaretIndex = preCaretTextRange.text.length;
}
return currentCaretIndex;
}
function getCurrentNodeIndexAtCaret(caretIndex) {
if (caretIndex == 0) {
return 0;
}
var currentNodeIndex = -1;
for (var i = 0; i < editor.childNodes.length; i++) {
var frontTextLength = getTextLengthFromStartTo(i);
var backTextLength = getTextLengthFromStartTo(i + 1);
if (caretIndex > frontTextLength && caretIndex <= backTextLength) {
currentNodeIndex = i;
break;
}
}
return currentNodeIndex;
}
function getCurrentTextIndexInNodexAtCaret(nodeIndex, caretIndex) {
var textLength = getTextLengthFromStartTo(nodeIndex);
var textIndex = caretIndex - textLength;
return (textIndex < 0) ? 0 : textIndex;
}
function clearSelection() {
if (window.getSelection().rangeCount > 0) {
if (lastRange != null) {
window.getSelection().removeAllRanges();
lastRange.collapse(true);
window.getSelection().addRange(lastRange);
}
}
}
function getTextNodesIn(node) {
var textNodes = [];
if (node.nodeType == 3) {
textNodes.push(node);
} else {
var children = node.childNodes;
for (var i = 0, len = children.length; i < len; ++i) {
textNodes.push.apply(textNodes, getTextNodesIn(children[i]));
}
}
return textNodes;
}
function setSelectionRange(el, start, end) {
if (document.createRange && window.getSelection) {
var range = document.createRange();
range.selectNodeContents(el);
var textNodes = getTextNodesIn(el);
var foundStart = false;
var charCount = 0, endCharCount;
for (var i = 0, textNode; textNode = textNodes[i++]; ) {
endCharCount = charCount + textNode.length;
if (!foundStart && start >= charCount && (start < endCharCount || (start == endCharCount && i <= textNodes.length))) {
range.setStart(textNode, start - charCount);
foundStart = true;
}
if (foundStart && end <= endCharCount) {
range.setEnd(textNode, end - charCount);
break;
}
charCount = endCharCount;
}
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
lastRange = range;
sel.anchorNode.parentElement.scrollIntoView();
} else if (document.selection && document.body.createTextRange) {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(true);
textRange.moveEnd("character", end);
textRange.moveStart("character", start);
textRange.select();
}
}
function findText(text, caseSensitive) {
var currentCaretIndex = getCurrentCaretIndex();
clearSelection();
var regex;
if (caseSensitive) {
regex = text;
} else {
regex = new RegExp(text, "gi");
}
var childNodes = editor.childNodes;
var startNodeIndex = getCurrentNodeIndexAtCaret(currentCaretIndex);
var endNodeIndex = childNodes.length;
var startTextIndex = 0;
if (window.getSelection().focusOffset == 1) {
startTextIndex = lastSearchedTextIndex + 1;
} else {
startTextIndex = getCurrentTextIndexInNodexAtCaret(startNodeIndex, currentCaretIndex);
}
var searchedTextIndex = -1;
var searchedNodeIndex = 0;
var searchTargetSentence = null;
var searchLoopCount = 0;
if (currentCaretIndex == editor.textContent.length) {
startNodeIndex = 0;
startTextIndex = 0;
}
do
{
for (var i = startNodeIndex; i < endNodeIndex; i++) {
if (typeof (childNodes[i].textContent) == undefined || childNodes[i].textContent == null) {
startTextIndex = 0;
continue;
}
if (startTextIndex == childNodes[i].textContent.length) {
startTextIndex = 0;
continue;
}
if (startTextIndex > 0) {
searchTargetSentence = childNodes[i].textContent.substring(startTextIndex, childNodes[i].textContent.length);
} else {
searchTargetSentence = childNodes[i].textContent;
}
searchedTextIndex = searchTargetSentence.search(regex);
if (searchedTextIndex > -1) {
searchedTextIndex += startTextIndex;
searchedNodeIndex = i;
break;
}
startTextIndex = 0;
}
if (searchedTextIndex == -1) {
endNodeIndex = startNodeIndex + 1;
startNodeIndex = 0;
searchLoopCount++;
}
} while (searchLoopCount < 2 && searchedTextIndex == -1);
lastsearchedNodeIndex = searchedNodeIndex;
lastSearchedTextIndex = searchedTextIndex;
if (searchedNodeIndex > -1 && searchedTextIndex > -1) {
var textStartIndex = getTextLengthFromStartTo(searchedNodeIndex) + searchedTextIndex;
setSelectionRange(editor, textStartIndex, textStartIndex + text.length);
return true;
} else {
return false;
}
}
function replaceText(textToFind, textToReplace, caseSensitive) {
if (findText(textToFind, caseSensitive) == true) {
var sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(textToReplace));
}
} else if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
range.text = textToReplace;
}
return true;
} else {
return false;
}
}
There are no replace text method in CefSharp.
I think you have two options
Implement in javascript/html in the browser (DIY or something
like Tiny)
Manipulating the html from C# using GetSourceAsync and LoadHtml (Documentation)
Second question - I think you may be able to at least read the hits using the FindHandler. Have not tested this myself.

Unity javascript array problems

I have next code
var D3s = new Array(9);
var D3n: float[];
result = Mine(result);
function Mine(block) {
D3n = new float[5];
var init = false;
if (block.startsWith("##new#")) {
block = block.replace("##new#", "");
init = true;
D3n[3] = 16;
}
var s1 = block.split("&");
s1.forEach(foreachBlocks);
if (init) {
//set default values
D3n[1] = 0;
D3s[6] = "ff";
D3n[2] = 0;
}
return TurnM79();
}
and error: CompilationErrorsException: script(9,9): BCE0005: Boo.Lang.Compiler.CompilerError: Unknown identifier: 'D3n'.
what wrong?

UI-GRID column summation on checkbox change

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.

Hittesting against a level building array

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.

For each string in Array

Just as the name says, I want that for each certain name in an array a value is added to a int.
For example: if there are 3 strings of the same name in the array, then 3 times 50 will be added to the value.
This is my script I have now:
var lootList = new Array();
var interaction : Texture;
var interact = false;
var position : Rect;
var ching : AudioClip;
var lootPrice = 0;
function Update()
{
print(lootList);
if ("chalice" in lootList){
lootPrice += 50;
}
}
function Start()
{
position = Rect( ( Screen.width - interaction.width ) /2, ( Screen.height - interaction.height ) /2, interaction.width, interaction.height );
}
function OnTriggerStay(col : Collider)
{
if(col.gameObject.tag == "loot")
{
interact = true;
if(Input.GetKeyDown("e"))
{
if(col.gameObject.name == "chalice")
{
Destroy(col.gameObject);
print("chaliceObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("chalice");
}
if(col.gameObject.name == "moneyPouch")
{
Destroy(col.gameObject);
print("moneyPouchObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("moneyPouch");
}
if(col.gameObject.name == "ring")
{
Destroy(col.gameObject);
print("ringObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("ring");
}
if(col.gameObject.name == "goldCoins")
{
Destroy(col.gameObject);
print("coldCoinsObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("goldCoins");
}
if(col.gameObject.name == "plate")
{
Destroy(col.gameObject);
print("plateObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("plate");
}
}
}
}
function OnTriggerExit(col : Collider)
{
if(col.gameObject.tag == "pouch")
{
interact = false;
}
}
function OnGUI()
{
if(interact == true)
{
GUI.DrawTexture(position, interaction);
GUI.color.a = 1;
}
}
It's for a game I'm making where you can steal items for extra score points.
I've tried using the for(i = 0; i < variable.Length; i++) but that didn't seem to work.
The only thing I can think of now is using booleans to add it once. But that isn't memory friendly.
Help is appreciated and thanks in advance!
You could use the standard .forEach(callback) method:
lootList.forEach(function(value, index, array)
{
if (value === "chalice") { lootPrice += 50; }
});
If you don't have that method, you could implement it like this:
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (callback) {
for(var i = 0; i < this.length; i++) { callback(this[i], i, this); }
}
}

Resources