This script should spawn object after clicking on other object which is closer than hitRange and has tag "Block". The problem is that it always spawns new object after pushing LMB, also when needed conditions weren't met.
#pragma strict
var blockName : String;
private static var blockToPlace : Rigidbody;
var hitRange : float;
private var hit : RaycastHit;
var blockLayer : int = 8;
private var hitBlock : boolean;
function Update(){
if(Input.GetMouseButtonDown(0)){
blockToPlace = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath("Assets/Models/Blocks/"+blockName+".prefab", Rigidbody)) as Rigidbody;
hitBlock = Physics.Raycast(transform.position, transform.forward, hit, Mathf.Infinity, blockLayer);
if(hitBlock == true){
if(hit.collider.tag == "Block"){
if(hit.distance < hitRange){
var block : Rigidbody;
block = Instantiate(blockToPlace,hit.normal+hit.transform.position,Quaternion.identity) as Rigidbody;
hitBlock = false;
}
}
}
}
}
I had a hard time with the same problem myself. Layers arn't exactly integers but rather bitshifts.
Watch this very nice video on layermasks: www.youtube.com/watch?v=n8mAjwn-MB8
var blockLayer : int = 8;
should be
var blockLayer : int = 1 << 8;
Related
I have the codeblock like this and I am trying to get rid of the Float should be Int and Missing Return errors.
package com.bykd.dev;
#:final class Version
{
public static inline var SPLIT_CHAR : String = ".";
public static var revisionKeyword : String = "Revision";
private var _tag : String;
private var _numbers : Array<Dynamic>;
public static function create(pfx : String, rev : String = null, sfx : String = null) : Version
{
var nums : Array<Dynamic> = null;
nums = pfx.split(SPLIT_CHAR);
if (rev != null)
{
nums.push(trimRevision(rev));
}
return new Version(nums, sfx);
private static function trimRevision(rev : String) : String
{
var beg : Float = Math.NaN;
var end : Float = Math.NaN;
beg = Std.string("$" + revisionKeyword + ": ").length;
end = rev.lastIndexOf(" $");
return rev.substring(beg, end);
}
}
Errors are in the last lines :
end = rev.lastIndexOf(" $");
return rev.substring(beg, end);
Any help would be highly appreciated.
Why use Float?
var beg : Int = 0;
var end : Int = 0;
Also avoid Dynamic when possible
var nums : Array<String> = null;
nums = pfx.split(SPLIT_CHAR);
How can I convert a haxe.Int64 to a Float?
I've got something like
var x = haxe.Int64.parseString("1000000000000");
and I'd like to convert that to a Float. I've looked in the Int64 api docs, have found fromFloat, ofInt, and toInt, but there's no toFloat method in there.
I cannot see that functionality in the Haxe standard library either, I checked Int64 and Int64Helper.
However the MIT-licensed thx.core library does include an implementation, see below: https://github.com/fponticelli/thx.core/blob/master/src/thx/Int64s.hx#L137
using haxe.Int64;
class Int64s {
static var zero = Int64.make(0, 0);
static var one = Int64.make(0, 1);
static var min = Int64.make(0x80000000, 0);
/**
Converts an `Int64` to `Float`;
Implementation by Elliott Stoneham.
*/
public static function toFloat(i : Int64) : Float {
var isNegative = false;
if(i < 0) {
if(i < min)
return -9223372036854775808.0; // most -ve value can't be made +ve
isNegative = true;
i = -i;
}
var multiplier = 1.0,
ret = 0.0;
for(_ in 0...64) {
if(i.and(one) != zero)
ret += multiplier;
multiplier *= 2.0;
i = i.shr(1);
}
return (isNegative ? -1 : 1) * ret;
}
}
Using the library method worked for me, tested on the JavaScript target like so:
import haxe.Int64;
import thx.Int64s;
class Main {
public static function main():Void {
var x:Int64 = haxe.Int64.parseString("1000000000000");
var f:Float = thx.Int64s.toFloat(x);
trace(f); // Prints 1000000000000 to console (on js target)
}
}
Alternatively, you can convert Int64 to Float yourself by combining high/low halves:
class Test {
static function main() {
var x = haxe.Int64.parseString("1000000000000");
var f = x.high * 4294967296. + (x.low >>> 0);
trace("" + x);
trace(f);
}
}
Here,
4294967296. is a float literal for unsigned 2^32;
>>> 0 is required because the lower half can be signed.
Or, for a naive conversion method, you can use:
var f = Std.parseFloat(haxe.Int64.toStr(x));
You may want to try to use FPHelper class. It contains a set of methods to convert Int types to Float / Double.
import haxe.io.FPHelper;
class Test {
static function main() {
var x = haxe.Int64.parseString("1000000000000");
var f = FPHelper.i64ToDouble(x.low, x.high);
trace(f);
}
}
See https://try.haxe.org/#CDa93
Assuming I have the following classes in Haxe:
class Pair<U, V> {
public var first:U = null;
public var second:V = null;
public function new(u:U, v:V) {
this.first = u;
this.second = v;
}
}
class HashablePair<U:{ function hashCode():Int; }, V:{ function hashCode():Int; }> {
public var first:U = null;
public var second:V = null;
public function new(u:U, v:V) {
this.first = u;
this.second = v;
}
public function hashCode():Int { // just a sample way ...
var h1:Int = (first == null) ? 0 : first.hashCode();
var h2:Int = (second == null) ? 0 : second.hashCode();
return 3 * h1 + 5 * h2;
}
}
I wondered if it is possible to write a macro that adds the hashCode function to the pair class, if and only if both generics U and V implement the hashCode-function ... and thus make it possible to combine the two classes into a single one via meta-programming.
You can achieve the desired behavior by simply switching to an abstract:
typedef Hashable = { function hashCode():Int; };
abstract HashablePair<U:Hashable,V:Hashable>(Pair<U,V>)from Pair<U,V> {
public function new(u:U, v:V)
this = new Pair(u, v);
public function hashCode():Int { // just a sample way ...
var h1:Int = (this.first == null) ? 0 : this.first.hashCode();
var h2:Int = (this.second == null) ? 0 : this.second.hashCode();
return 3 * h1 + 5 * h2;
}
}
The from Pair<U,V> makes Pair<U,V> casts to HashablePair<U,V> allowed, as long as the necessary constrains on U and V are respected.
For a complete exemple, check out Try Haxe #d76E1.
I have a sound that gets triggered with OnTriggerEnter, and picking up whether sound is playing to turn sprite renderer on/off.
For some reason, when I start the game, the sound starts playing (which should only be getting called by the trigger).
I checked the trigger with the players tag and debug.log to console, and found that the trigger method isn't being called at the start. But, for some reason, the sound is getting played at the start and when I hit the trigger(but not turning the sprite renderer back on).
FlagScript
#pragma strict
var Sprite2 : Sprite; //second sprite to renderer
var sr : SpriteRenderer;
var hasBeenHit : boolean = false;
var flagCollisionSounderies: AudioSource;
var flagCollisionSound : AudioClip = null;
private var applauseRef: ApplauseScript;
private var go: GameObject;
var count : int = 0;
function Start () {
//access the spriteRenderer
flagCollisionSound = flagCollisionSounderies.clip;
sr = GetComponent(SpriteRenderer);
go = GameObject.Find("ClapMaster");
applauseRef = go.GetComponent(typeof(ApplauseScript));
//Debug.Log("bool: "+flagCollisionSounderies.isPlaying); //true already
}
function OnTriggerEnter2D(coll: Collider2D){
//Debug.Log("flagtriggered");
//change the flag to other flag
sr.sprite = Sprite2;
if (coll.gameObject.tag == "Player"){
Debug.Log("poos");
if (hasBeenHit == false){
GameMaster.setFlags(GameMaster.getFlags()-1);
GameMaster.addPointsToScore(100);
if (flagCollisionSound != null){
flagCollisionSounderies.PlayClipAtPoint(flagCollisionSound, transform.position);
//Debug.Log("bool:" + flagCollisionSounderies.isPlaying);
applauseRef.animRenderer.enabled = true;
}
}
}
hasBeenHit = true;
}
function Update(){
if(!flagCollisionSounderies.isPlaying){
applauseRef.animRenderer.enabled = false;
//applauseRef.playApplauseAnim = false;
}else{
//Debug.Log("sounderies detecteddFsdlfkjsdlfksjdflksdjflkj1123123");
applauseRef.animRenderer.enabled = true;
//applauseRef.playApplauseAnim = true;
}
}
Applause Script
#pragma strict
var SpriteLoad: Sprite[];
static var animSprites : Sprite[];
static var playerScriptRef: newplayer;
static var animRenderer: SpriteRenderer;
//private static var i : int = 7;
private static var frame : int = 3;
private static var Ymove: float = 0f;
private static var goingUp: boolean = false;
static var playApplauseAnim: boolean;
function Start(){
animRenderer = GetComponent(SpriteRenderer);
animSprites = SpriteLoad;
playerScriptRef = GameObject.Find("Player 1").GetComponent(newplayer);
//animRenderer.enabled = false;
//playApplauseAnim = false;
}
function Update(){
PicMover();
frame = (frame +1 ) % 4;
animRenderer.sprite = animSprites[frame];
}
static function PicMover(){
if(Ymove < 20 && goingUp){
Ymove++;
}else if(Ymove > -20 && !goingUp){
Ymove--;
}else if(Ymove == 20){
goingUp = false;
}else if(Ymove == -20){
goingUp = true;
}
animRenderer.transform.position = new Vector2(10,10);
}
one class sample : testHTTP.class in this code
public String httptest(object httptestx)
{
var data = (Tuple<string, string, string>)httptestx;
var s = data.Item1;
var k = data.Item2;
var p = data.Item3;
............................
}
Form1 thread class not start please help me ... :
private void button5_Click(object sender, EventArgs e)
{
for (Int32 i5 = 0; i5 < textBox5.Lines.Length; i5++)
{
var thr1 = new Thread(dfproJesiHttpClass.httptest());
var data = new Tuple<string, string, string>(textBox6.Lines[i6].Trim(), textBox4.Lines[i4].Trim(), textBox5.Lines[i5].Trim());
thr1.Start(data);
threads.Add(thr1);
}
}
I'd be surprised if that code compiles. You need to change this line:
var thr1 = new Thread(dfproJesiHttpClass.httptest());
to
var thr1 = new Thread(dfproJesiHttpClass.httptest);
Assuming that dfproJesiHttpClass is an instance and not the name of the class.